History

[utils #154] refactor: bool.then(||this) - neat

Should keep practicing its usage so that this wonderful code multiplies.

Sebastian Thiel committed 5 years ago

Commit 1dec1c41 parent ae4fb9c6 files · +8 −32Browse files

Unified · Split

6 files
  1. etc/utils/src/command/release.rs +1 −5
  2. git-odb/src/store/loose/iter.rs +1 −5
  3. git-pack/src/data/input/bytes_to_entries.rs +3 −5
  4. git-pack/src/data/input/lookup_ref_delta_objects.rs +1 −5
  5. git-pack/src/data/output/count/iter_from_objects.rs +1 −5
  6. git-tempfile/src/fs/remove_dir.rs +1 −7
etc/utils/src/command/release.rs+1−5
@@ -137,11 +137,7 @@ fn find_directory_id_in_tree( · 136 unmodified lines
137137 tree_id = tree_iter
138138 .find_map(|e| {
139139 let e = e.expect("tree parseable");
140− if e.filename == c {
141− Some(e.oid)
142− } else {
143− None
144− }
140+ (e.filename == c).then(|| e.oid)
145141 })
146142 .map(ToOwned::to_owned);
147143 if tree_id.is_none() {
git-odb/src/store/loose/iter.rs+1−5
@@ -45,11 +45,7 @@ impl Store { · 44 unmodified lines
4545 }
4646 git_hash::ObjectId::null_sha1()
4747 });
48− if is_valid_path {
49− Some(e)
50− } else {
51− None
52− }
48+ is_valid_path.then(|| e)
5349 }
5450
5551 /// Return an iterator over all objects contained in the database.
git-pack/src/data/input/bytes_to_entries.rs+3−5
@@ -61,13 +61,11 @@ where · 60 unmodified lines
6161 had_error: false,
6262 kind,
6363 objects_left: num_objects,
64− hash: if mode != input::Mode::AsIs {
64+ hash: (mode != input::Mode::AsIs).then(|| {
6565 let mut hash = Sha1::default();
6666 hash.update(&header_data);
67− Some(hash)
68− } else {
69− None
70− },
67+ hash
68+ }),
7169 mode,
7270 compressed_buf: None,
7371 })
git-pack/src/data/input/lookup_ref_delta_objects.rs+1−5
@@ -149,11 +149,7 @@ where · 148 unmodified lines
149149 self.inserted_entry_length_at_offset
150150 .get(maybe_index_of_actual_entry)
151151 .and_then(|c| {
152− if c.pack_offset == base_pack_offset {
153− Some(maybe_index_of_actual_entry)
154− } else {
155− None
156− }
152+ (c.pack_offset == base_pack_offset).then(|| maybe_index_of_actual_entry)
157153 })
158154 .unwrap_or(index)
159155 };
git-pack/src/data/output/count/iter_from_objects.rs+1−5
@@ -411,11 +411,7 @@ mod util { · 410 unmodified lines
411411 break;
412412 }
413413 }
414− if res.is_empty() {
415− None
416− } else {
417− Some(res)
418− }
414+ (!res.is_empty()).then(|| res)
419415 }
420416 }
421417 }
git-tempfile/src/fs/remove_dir.rs+1−7
@@ -52,13 +52,7 @@ impl<'a> Iterator for Iter<'a> { · 51 unmodified lines
5252 },
5353 };
5454 self.cursor = match dir.parent() {
55− Some(parent) => {
56− if parent == self.boundary {
57− None
58− } else {
59− Some(parent)
60− }
61− },
55+ Some(parent) => (parent != self.boundary).then(||parent),
6256 None => unreachable!("directory {:?} ran out of parents, this really shouldn't happen before hitting the boundary {:?}"),
6357 };
6458 next