Skip to content

Resolve clippy::fallible_impl_from #1771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ build --@rules_rust//:clippy_flag=-Wclippy::use_debug
build --@rules_rust//:clippy_flag=-Dclippy::missing_const_for_fn

# Nursery overrides that are either buggy or we want to fix
build --@rules_rust//:clippy_flag=-Aclippy::fallible_impl_from # TODO(jhpratt) Flip
build --@rules_rust//:clippy_flag=-Aclippy::iter_with_drain # TODO(jhpratt): Flip
build --@rules_rust//:clippy_flag=-Aclippy::option_if_let_else # rust-lang/rust-clippy#8829
build --@rules_rust//:clippy_flag=-Aclippy::redundant_pub_crate # rust-lang/rust-clippy#5369
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ use-debug = "warn"
missing-const-for-fn = "deny"

# Nursery overrides that are either buggy or we want to fix
fallible-impl-from = { level = "allow", priority = 1 } # TODO(jhpratt) Flip
iter-with-drain = { level = "allow", priority = 1 } # TODO(jhpratt) Flip
option-if-let-else = { level = "allow", priority = 1 } # rust-lang/rust-clippy#8829
redundant-pub-crate = { level = "allow", priority = 1 } # rust-lang/rust-clippy#5369
Expand Down
2 changes: 1 addition & 1 deletion nativelink-scheduler/tests/cache_lookup_scheduler_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn make_cache_scheduler() -> Result<TestContext, Error> {
async fn add_action_handles_skip_cache() -> Result<(), Error> {
let context = make_cache_scheduler()?;
let action_info = make_base_action_info(UNIX_EPOCH, DigestInfo::zero_digest());
let action_result = ProtoActionResult::from(ActionResult::default());
let action_result = ProtoActionResult::try_from(ActionResult::default())?;
context
.ac_store
.update_oneshot(action_info.digest(), action_result.encode_to_vec().into())
Expand Down
139 changes: 79 additions & 60 deletions nativelink-util/src/action_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,19 +424,20 @@ pub struct FileInfo {
pub is_executable: bool,
}

//TODO: Make this TryFrom.
impl From<FileInfo> for FileNode {
fn from(val: FileInfo) -> Self {
let NameOrPath::Name(name) = val.name_or_path else {
panic!(
impl TryFrom<FileInfo> for FileNode {
type Error = Error;

fn try_from(val: FileInfo) -> Result<Self, Error> {
match val.name_or_path {
NameOrPath::Path(_) => Err(make_input_err!(
"Cannot return a FileInfo that uses a NameOrPath::Path(), it must be a NameOrPath::Name()"
);
};
Self {
name,
digest: Some((&val.digest).into()),
is_executable: val.is_executable,
node_properties: Option::default(), // Not supported.
)),
NameOrPath::Name(name) => Ok(Self {
name,
digest: Some((&val.digest).into()),
is_executable: val.is_executable,
node_properties: None, // Not supported.
}),
}
}
}
Expand All @@ -456,20 +457,21 @@ impl TryFrom<OutputFile> for FileInfo {
}
}

//TODO: Make this TryFrom.
impl From<FileInfo> for OutputFile {
fn from(val: FileInfo) -> Self {
let NameOrPath::Path(path) = val.name_or_path else {
panic!(
impl TryFrom<FileInfo> for OutputFile {
type Error = Error;

fn try_from(val: FileInfo) -> Result<Self, Error> {
match val.name_or_path {
NameOrPath::Name(_) => Err(make_input_err!(
"Cannot return a FileInfo that uses a NameOrPath::Name(), it must be a NameOrPath::Path()"
);
};
Self {
path,
digest: Some((&val.digest).into()),
is_executable: val.is_executable,
contents: Bytes::default(),
node_properties: Option::default(), // Not supported.
)),
NameOrPath::Path(path) => Ok(Self {
path,
digest: Some((&val.digest).into()),
is_executable: val.is_executable,
contents: Bytes::default(),
node_properties: None, // Not supported.
}),
}
}
}
Expand All @@ -493,18 +495,19 @@ impl TryFrom<SymlinkNode> for SymlinkInfo {
}
}

// TODO: Make this TryFrom.
impl From<SymlinkInfo> for SymlinkNode {
fn from(val: SymlinkInfo) -> Self {
let NameOrPath::Name(name) = val.name_or_path else {
panic!(
impl TryFrom<SymlinkInfo> for SymlinkNode {
type Error = Error;

fn try_from(val: SymlinkInfo) -> Result<Self, Error> {
match val.name_or_path {
NameOrPath::Path(_) => Err(make_input_err!(
"Cannot return a SymlinkInfo that uses a NameOrPath::Path(), it must be a NameOrPath::Name()"
);
};
Self {
name,
target: val.target,
node_properties: Option::default(), // Not supported.
)),
NameOrPath::Name(name) => Ok(Self {
name,
target: val.target,
node_properties: None, // Not supported.
}),
}
}
}
Expand All @@ -520,18 +523,21 @@ impl TryFrom<OutputSymlink> for SymlinkInfo {
}
}

// TODO: Make this TryFrom.
impl From<SymlinkInfo> for OutputSymlink {
fn from(val: SymlinkInfo) -> Self {
let NameOrPath::Path(path) = val.name_or_path else {
panic!(
impl TryFrom<SymlinkInfo> for OutputSymlink {
type Error = Error;

fn try_from(val: SymlinkInfo) -> Result<Self, Error> {
match val.name_or_path {
NameOrPath::Path(path) => {
Ok(Self {
path,
target: val.target,
node_properties: None, // Not supported.
})
}
NameOrPath::Name(_) => Err(make_input_err!(
"Cannot return a SymlinkInfo that uses a NameOrPath::Path(), it must be a NameOrPath::Name()"
);
};
Self {
path,
target: val.target,
node_properties: Option::default(), // Not supported.
)),
}
}
}
Expand Down Expand Up @@ -850,7 +856,7 @@ pub fn to_execute_response(action_result: ActionResult) -> ExecuteResponse {
let message = action_result.message.clone();
ExecuteResponse {
server_logs: logs_from(action_result.server_logs.clone()),
result: Some(action_result.into()),
result: action_result.try_into().ok(),
cached_result: false,
status,
message,
Expand Down Expand Up @@ -880,35 +886,48 @@ impl From<ActionStage> for ExecuteResponse {
}
}

impl From<ActionResult> for ProtoActionResult {
fn from(val: ActionResult) -> Self {
impl TryFrom<ActionResult> for ProtoActionResult {
type Error = Error;

fn try_from(val: ActionResult) -> Result<Self, Error> {
let mut output_symlinks = Vec::with_capacity(
val.output_file_symlinks.len() + val.output_directory_symlinks.len(),
);
output_symlinks.extend_from_slice(val.output_file_symlinks.as_slice());
output_symlinks.extend_from_slice(val.output_directory_symlinks.as_slice());

Self {
output_files: val.output_files.into_iter().map(Into::into).collect(),
Ok(Self {
output_files: val
.output_files
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
output_file_symlinks: val
.output_file_symlinks
.into_iter()
.map(Into::into)
.collect(),
output_symlinks: output_symlinks.into_iter().map(Into::into).collect(),
output_directories: val.output_folders.into_iter().map(Into::into).collect(),
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
output_symlinks: output_symlinks
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
output_directories: val
.output_folders
.into_iter()
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
output_directory_symlinks: val
.output_directory_symlinks
.into_iter()
.map(Into::into)
.collect(),
.map(TryInto::try_into)
.collect::<Result<_, _>>()?,
exit_code: val.exit_code,
stdout_raw: Bytes::default(),
stdout_digest: Some(val.stdout_digest.into()),
stderr_raw: Bytes::default(),
stderr_digest: Some(val.stderr_digest.into()),
execution_metadata: Some(val.execution_metadata.into()),
}
})
}
}

Expand Down Expand Up @@ -1026,7 +1045,7 @@ impl TryFrom<ExecuteResponse> for ActionStage {
};

if execute_response.cached_result {
return Ok(Self::CompletedFromCache(action_result.into()));
return Ok(Self::CompletedFromCache(action_result.try_into()?));
}
Ok(Self::Completed(action_result))
}
Expand Down
10 changes: 6 additions & 4 deletions nativelink-worker/src/running_actions_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,14 @@ fn upload_directory<'a, P: AsRef<Path> + Debug + Send + Sync + Clone + 'a>(
.await
.err_tip(|| format!("Could not open file {full_path:?}"))?;
upload_file(cas_store, &full_path, hasher, metadata)
.map_ok(Into::into)
.await
.map_ok(TryInto::try_into)
.await?
});
} else if file_type.is_symlink() {
symlink_futures
.push(upload_symlink(full_path, &full_work_directory).map_ok(Into::into));
symlink_futures.push(
upload_symlink(full_path, &full_work_directory)
.map(|symlink| symlink?.try_into()),
);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions nativelink-worker/tests/running_actions_manager_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2066,7 +2066,7 @@ async fn caches_results_in_action_cache_store() -> Result<(), Box<dyn core::erro
let retrieved_result =
get_and_decode_digest::<ProtoActionResult>(ac_store.as_ref(), action_digest.into()).await?;

let proto_result: ProtoActionResult = action_result.into();
let proto_result: ProtoActionResult = action_result.try_into()?;
assert_eq!(proto_result, retrieved_result);

Ok(())
Expand Down Expand Up @@ -2138,7 +2138,7 @@ async fn failed_action_does_not_cache_in_action_cache() -> Result<(), Box<dyn co
let retrieved_result =
get_and_decode_digest::<ProtoActionResult>(ac_store.as_ref(), action_digest.into()).await?;

let proto_result: ProtoActionResult = action_result.into();
let proto_result: ProtoActionResult = action_result.try_into()?;
assert_eq!(proto_result, retrieved_result);

Ok(())
Expand Down Expand Up @@ -2237,7 +2237,7 @@ async fn success_does_cache_in_historical_results() -> Result<(), Box<dyn core::
HistoricalExecuteResponse {
action_digest: Some(action_digest.into()),
execute_response: Some(ExecuteResponse {
result: Some(action_result.into()),
result: Some(action_result.try_into()?),
status: Some(Status::default()),
..Default::default()
}),
Expand Down Expand Up @@ -2351,7 +2351,7 @@ async fn infra_failure_does_cache_in_historical_results() -> Result<(), Box<dyn
HistoricalExecuteResponse {
action_digest: Some(action_digest.into()),
execute_response: Some(ExecuteResponse {
result: Some(action_result.into()),
result: Some(action_result.try_into()?),
status: Some(make_input_err!("test error").into()),
..Default::default()
}),
Expand Down Expand Up @@ -2407,7 +2407,7 @@ async fn action_result_has_used_in_message() -> Result<(), Box<dyn core::error::
get_and_decode_digest::<ProtoActionResult>(ac_store.as_ref(), action_result_digest.into())
.await?;

let proto_result: ProtoActionResult = action_result.into();
let proto_result: ProtoActionResult = action_result.try_into()?;
assert_eq!(proto_result, retrieved_result);
Ok(())
}
Expand Down
Loading