Skip to content

Commit 3fa1790

Browse files
committed
refactor(rustup-mode): introduce ExitCode::bitand*()
1 parent 1d015e1 commit 3fa1790

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/cli/common.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,7 @@ pub(crate) async fn update_all_channels(
314314
};
315315

316316
if do_self_update {
317-
let self_update_exit_code = self_update(show_channel_updates, cfg.process).await?;
318-
if self_update_exit_code != utils::ExitCode(0) {
319-
exit_code = self_update_exit_code;
320-
}
317+
exit_code &= self_update(show_channel_updates, cfg.process).await?;
321318
} else {
322319
show_channel_updates()?;
323320
}

src/cli/rustup_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode>
867867
common::self_update(|| Ok(()), cfg.process).await?;
868868
}
869869
} else {
870-
exit_code = common::update_all_channels(cfg, self_update, opts.force).await?;
870+
exit_code &= common::update_all_channels(cfg, self_update, opts.force).await?;
871871
info!("cleaning up downloads & tmp directories");
872872
utils::delete_dir_contents_following_links(&cfg.download_dir);
873873
cfg.tmp_cx.clean();

src/utils/utils.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::env;
22
use std::fs::{self, File};
33
use std::io::{self, BufReader, Write};
4+
use std::ops::{BitAnd, BitAndAssign};
45
use std::path::{Path, PathBuf};
56
use std::process::ExitStatus;
67

@@ -24,6 +25,27 @@ pub use crate::utils::utils::raw::{is_file, path_exists};
2425
#[derive(Debug, PartialEq, Eq)]
2526
pub struct ExitCode(pub i32);
2627

28+
impl BitAnd for ExitCode {
29+
type Output = Self;
30+
31+
// If `self` is `0` (success), yield `rhs`.
32+
fn bitand(self, rhs: Self) -> Self::Output {
33+
match self.0 {
34+
0 => rhs,
35+
_ => self,
36+
}
37+
}
38+
}
39+
40+
impl BitAndAssign for ExitCode {
41+
// If `self` is `0` (success), set `self` to `rhs`.
42+
fn bitand_assign(&mut self, rhs: Self) {
43+
if self.0 == 0 {
44+
*self = rhs
45+
}
46+
}
47+
}
48+
2749
impl From<ExitStatus> for ExitCode {
2850
fn from(status: ExitStatus) -> Self {
2951
Self(match status.success() {

0 commit comments

Comments
 (0)