From 06c254e7427ec1f736b20bb7d84e6eddf9ca448c Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 30 Oct 2023 06:33:14 +0900 Subject: [PATCH] git_backend: use non-owned str::from_utf8() to decode symlink target Just for consistency with the other changes. str::Utf8Error is 2 words long, so I removed the boxing. --- lib/src/backend.rs | 4 +--- lib/src/git_backend.rs | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/src/backend.rs b/lib/src/backend.rs index 0c50bc6d4..91c0f6a01 100644 --- a/lib/src/backend.rs +++ b/lib/src/backend.rs @@ -251,9 +251,7 @@ pub enum BackendError { InvalidUtf8 { object_type: String, hash: String, - // Box to reduce size for other error types that include this one: - // https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err - source: Box, + source: std::str::Utf8Error, }, #[error("Object {hash} of type {object_type} not found: {source}")] ObjectNotFound { diff --git a/lib/src/git_backend.rs b/lib/src/git_backend.rs index ff3a85445..7a846608e 100644 --- a/lib/src/git_backend.rs +++ b/lib/src/git_backend.rs @@ -16,11 +16,11 @@ use std::any::Any; use std::fmt::{Debug, Error, Formatter}; -use std::fs; use std::io::{Cursor, Read}; use std::ops::Deref; use std::path::{Path, PathBuf}; use std::sync::{Arc, Mutex, MutexGuard}; +use std::{fs, str}; use async_trait::async_trait; use git2::Oid; @@ -468,6 +468,14 @@ fn map_not_found_err(err: git2::Error, id: &impl ObjectId) -> BackendError { } } +fn to_invalid_utf8_err(source: str::Utf8Error, id: &impl ObjectId) -> BackendError { + BackendError::InvalidUtf8 { + object_type: id.object_type(), + hash: id.hex(), + source, + } +} + fn import_extra_metadata_entries_from_heads( git_repo: &git2::Repository, mut_table: &mut MutableTable, @@ -556,13 +564,9 @@ impl Backend for GitBackend { let blob = locked_repo .find_blob(git_blob_id) .map_err(|err| map_not_found_err(err, id))?; - let target = String::from_utf8(blob.content().to_owned()).map_err(|err| { - BackendError::InvalidUtf8 { - object_type: id.object_type(), - hash: id.hex(), - source: Box::new(err), - } - })?; + let target = str::from_utf8(blob.content()) + .map_err(|err| to_invalid_utf8_err(err, id))? + .to_owned(); Ok(target) }