From 9a60751a901f568bdeb154c4115235d4f3a0f8b9 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Fri, 20 Mar 2026 21:54:29 +0800 Subject: Apply clippy suggestions and improve code quality --- systems/sheet/macros/src/lib.rs | 4 ++-- systems/sheet/src/compare.rs | 2 +- systems/sheet/src/index_source.rs | 10 ++-------- systems/sheet/src/index_source/alias.rs | 15 +++++++-------- systems/sheet/src/lazy.rs | 4 ++-- systems/sheet/src/mapping.rs | 32 ++++++++++---------------------- systems/sheet/src/mapping/parse.rs | 4 +--- systems/sheet/src/sheet.rs | 29 +++++++++++++---------------- systems/sheet/src/sheet/v1/constants.rs | 12 ++++-------- systems/sheet/src/sheet/v1/writer.rs | 9 +++------ 10 files changed, 45 insertions(+), 76 deletions(-) (limited to 'systems/sheet') diff --git a/systems/sheet/macros/src/lib.rs b/systems/sheet/macros/src/lib.rs index 2990f7e..ea2b139 100644 --- a/systems/sheet/macros/src/lib.rs +++ b/systems/sheet/macros/src/lib.rs @@ -41,8 +41,8 @@ fn parse_id_version(input: &str) -> Result<(bool, u32, u16), String> { let trimmed = input.trim(); // Check if it starts with ~ for local - let (remote, id_part) = if trimmed.starts_with('~') { - (false, &trimmed[1..]) + let (remote, id_part) = if let Some(stripped) = trimmed.strip_prefix('~') { + (false, stripped) } else { (true, trimmed) }; diff --git a/systems/sheet/src/compare.rs b/systems/sheet/src/compare.rs index b5b44b4..95b1aa3 100644 --- a/systems/sheet/src/compare.rs +++ b/systems/sheet/src/compare.rs @@ -7,7 +7,7 @@ use std::cmp::Ordering; /// 4. All other Unicode characters (in their natural order) /// /// The comparison is lexicographic: the first differing element determines the order. -pub fn compare_vec_string(a: &Vec, b: &Vec) -> std::cmp::Ordering { +pub fn compare_vec_string(a: &[String], b: &[String]) -> std::cmp::Ordering { use std::cmp::Ordering; for (left, right) in a.iter().zip(b.iter()) { diff --git a/systems/sheet/src/index_source.rs b/systems/sheet/src/index_source.rs index 43508f2..b11a6b6 100644 --- a/systems/sheet/src/index_source.rs +++ b/systems/sheet/src/index_source.rs @@ -62,7 +62,7 @@ impl IndexSource { impl PartialEq for IndexSource { fn eq(&self, other: &Self) -> bool { - &self.remote == &other.remote && &self.id == &other.id && &self.ver == &other.ver + self.remote == other.remote && self.id == other.id && self.ver == other.ver } } @@ -158,12 +158,6 @@ impl IndexSource { impl std::fmt::Display for IndexSource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let local_symbol = if self.remote { "" } else { "~" }; - write!( - f, - "{}{}/{}", - local_symbol, - self.id.to_string(), - self.ver.to_string() - ) + write!(f, "{}{}/{}", local_symbol, self.id, self.ver) } } diff --git a/systems/sheet/src/index_source/alias.rs b/systems/sheet/src/index_source/alias.rs index d50c183..8b2cd5b 100644 --- a/systems/sheet/src/index_source/alias.rs +++ b/systems/sheet/src/index_source/alias.rs @@ -139,31 +139,30 @@ async fn get_or_create_alias_file( .read(true) .write(true) .create(true) + .truncate(false) .open(&file_path) .await - .map_err(|e| IDAliasError::Io(e))?; + .map_err(IDAliasError::Io)?; - let metadata = file.metadata().await.map_err(|e| IDAliasError::Io(e))?; + let metadata = file.metadata().await.map_err(IDAliasError::Io)?; if metadata.len() != FILE_SIZE { drop(file); let file = OpenOptions::new() .write(true) .create(true) - .truncate(true) + .truncate(false) .open(&file_path) .await - .map_err(|e| IDAliasError::Io(e))?; + .map_err(IDAliasError::Io)?; - file.set_len(FILE_SIZE) - .await - .map_err(|e| IDAliasError::Io(e))?; + file.set_len(FILE_SIZE).await.map_err(IDAliasError::Io)?; let file = OpenOptions::new() .read(true) .write(true) .open(&file_path) .await - .map_err(|e| IDAliasError::Io(e))?; + .map_err(IDAliasError::Io)?; Ok(file) } else { diff --git a/systems/sheet/src/lazy.rs b/systems/sheet/src/lazy.rs index 2278704..c9ba9df 100644 --- a/systems/sheet/src/lazy.rs +++ b/systems/sheet/src/lazy.rs @@ -8,13 +8,13 @@ macro_rules! lazy_node { #[macro_export] macro_rules! lazy_idx { ($idx:expr, $ver:expr) => { - crate::index_source::IndexSource::new(false, $idx, $ver) + $crate::index_source::IndexSource::new(false, $idx, $ver) }; } #[macro_export] macro_rules! lazy_ridx { ($idx:expr, $ver:expr) => { - crate::index_source::IndexSource::new(true, $idx, $ver) + $crate::index_source::IndexSource::new(true, $idx, $ver) }; } diff --git a/systems/sheet/src/mapping.rs b/systems/sheet/src/mapping.rs index 2e6645e..c1bbafd 100644 --- a/systems/sheet/src/mapping.rs +++ b/systems/sheet/src/mapping.rs @@ -171,7 +171,7 @@ impl LocalMapping { /// Clone and generate a MappingBuf from LocalMapping pub fn to_mapping_buf_cloned(&self, sheet_name: impl Into) -> MappingBuf { - MappingBuf::new(sheet_name.into(), self.val.clone(), self.source.clone()) + MappingBuf::new(sheet_name.into(), self.val.clone(), self.source) } /// Generate a MappingBuf from LocalMapping @@ -235,7 +235,7 @@ impl MappingBuf { /// /// Additionally, if the length of the given forward value exceeds `u8::MAX`, it will also return None pub fn to_local_mapping_cloned(&self, forward: &LocalMappingForward) -> Option { - LocalMapping::new(self.val.clone(), self.source.clone(), forward.clone()) + LocalMapping::new(self.val.clone(), self.source, forward.clone()) } /// Generate a LocalMapping from MappingBuf @@ -262,7 +262,7 @@ impl<'a> Mapping<'a> { /// Get the sheet name of Mapping pub fn sheet_name(&self) -> &str { - &self.sheet_name + self.sheet_name } /// Build a Vec of Mapping values from the stored address @@ -276,7 +276,7 @@ impl<'a> Mapping<'a> { /// Get the value str of Mapping pub fn value_str(&self) -> &str { - &self.val + self.val } /// Get the IndexSource of Mapping @@ -301,7 +301,6 @@ impl<'a> Mapping<'a> { fmt_path_str(self.val) .unwrap_or_default() .split('/') - .into_iter() .map(|s| s.to_string()) .collect(), self.source, @@ -320,7 +319,6 @@ impl<'a> Mapping<'a> { fmt_path_str(self.val) .unwrap_or_default() .split("/") - .into_iter() .map(|s| s.to_string()) .collect(), self.source, @@ -404,36 +402,26 @@ impl std::fmt::Display for LocalMapping { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self.forward { LocalMappingForward::Latest => { - write!( - f, - "\"{}\" => \"{}\"", - self.val.join("/"), - self.source.to_string() - ) + write!(f, "\"{}\" => \"{}\"", self.val.join("/"), self.source) } LocalMappingForward::Ref { sheet_name } => { write!( f, "\"{}\" => \"{}\" => \"{}\"", self.val.join("/"), - self.source.to_string(), + self.source, sheet_name ) } LocalMappingForward::Version { version } => { if &self.mapped_version() == version { - write!( - f, - "\"{}\" == \"{}\"", - self.val.join("/"), - self.source.to_string(), - ) + write!(f, "\"{}\" == \"{}\"", self.val.join("/"), self.source,) } else { write!( f, "\"{}\" => \"{}\" == \"{}\"", self.val.join("/"), - self.source.to_string(), + self.source, version ) } @@ -556,7 +544,7 @@ fn join_helper(nodes: String, mut mapping_buf_val: Vec) -> Vec { } } - return mapping_buf_val; + mapping_buf_val } // Implement mutual comparison for MappingBuf and Mapping @@ -637,7 +625,7 @@ impl std::borrow::Borrow> for MappingBuf { /// ``` pub fn node(str: impl Into) -> Vec { let str = just_fmt::fmt_path::fmt_path_str(str).unwrap_or_default(); - str.split("/").into_iter().map(|f| f.to_string()).collect() + str.split("/").map(|f| f.to_string()).collect() } // Implement comparison for LocalMapping diff --git a/systems/sheet/src/mapping/parse.rs b/systems/sheet/src/mapping/parse.rs index e203c96..7460ffd 100644 --- a/systems/sheet/src/mapping/parse.rs +++ b/systems/sheet/src/mapping/parse.rs @@ -178,9 +178,7 @@ impl TryFrom<&str> for LocalMapping { Ok(LocalMapping { val, source, - forward: LocalMappingForward::Ref { - sheet_name: sheet_name, - }, + forward: LocalMappingForward::Ref { sheet_name }, }) } _ => Err(ParseMappingError::InvalidMapping), diff --git a/systems/sheet/src/sheet.rs b/systems/sheet/src/sheet.rs index fdcdd67..96fca9a 100644 --- a/systems/sheet/src/sheet.rs +++ b/systems/sheet/src/sheet.rs @@ -1,7 +1,6 @@ use std::{ collections::HashSet, fs::File, - mem::replace, path::{Path, PathBuf}, }; @@ -139,14 +138,14 @@ impl std::fmt::Display for SheetEditItem { write!(f, "erase \"{}\"", display_node_helper(node),) } SheetEditItem::InsertMapping { mapping } => { - write!(f, "Insert {}", mapping.to_string()) + write!(f, "Insert {}", mapping) } SheetEditItem::ReplaceSource { node, source } => { write!( f, "Replace \"{}\" => \"{}\"", display_node_helper(node), - source.to_string() + source ) } SheetEditItem::UpdateForward { node, forward } => match forward { @@ -177,7 +176,7 @@ impl std::fmt::Display for SheetEditItem { } #[inline(always)] -fn display_node_helper(n: &Vec) -> String { +fn display_node_helper(n: &[String]) -> String { n.join("/") } @@ -201,7 +200,7 @@ impl SheetData { } /// Load MMAP from a Sheet file - pub fn mmap<'a>(sheet_file: impl AsRef) -> std::io::Result { + pub fn mmap(sheet_file: impl AsRef) -> std::io::Result { let file = File::open(sheet_file.as_ref())?; // SAFETY: The file has been successfully opened and is managed by the SheetDataMmap wrapper @@ -244,7 +243,7 @@ impl SheetDataMmap { } /// Load mapping information from Sheet file at high speed and copy into LocalMapping - pub fn mp_c<'a>(&self, node: &[&str]) -> Result, ReadSheetDataError> { + pub fn mp_c(&self, node: &[&str]) -> Result, ReadSheetDataError> { match self.mp(node)? { Some((mapping, forward)) => { // Note: @@ -291,10 +290,8 @@ impl Sheet { /// Read from Sheet data and clone into MappingBuf pub fn read_mapping_buf(&self, value: &Vec) -> Option { - match self.read_local_mapping(value) { - Some(v) => Some(v.to_mapping_buf_cloned(&self.name)), - None => None, - } + self.read_local_mapping(value) + .map(|v| v.to_mapping_buf_cloned(&self.name)) } /// Insert mapping move @@ -410,7 +407,7 @@ impl Sheet { /// Apply changes pub fn apply(&mut self) -> Result<(), SheetApplyError> { - let items = replace(&mut self.edit.list, Vec::new()); + let items = std::mem::take(&mut self.edit.list); for item in items { match item { SheetEditItem::DoNothing => continue, @@ -516,9 +513,9 @@ impl TryFrom<&str> for SheetData { continue; } let mapping = LocalMapping::try_from(line)?; - let _ = sheet.insert_mapping(mapping)?; + sheet.insert_mapping(mapping)?; } - let _ = sheet.apply()?; + sheet.apply()?; Ok(sheet.unpack()) } } @@ -552,7 +549,7 @@ impl TryFrom<&[u8]> for SheetData { } impl RWData for SheetData { - async fn read(path: &PathBuf) -> Result { + async fn read(path: &Path) -> Result { let mut data = SheetData::empty(); data.full_read(path).await.map_err(|e| match e { ReadSheetDataError::IOErr(error) => DataReadError::IoError(error), @@ -562,11 +559,11 @@ impl RWData for SheetData { async fn write( data: SheetData, - path: &PathBuf, + path: &Path, ) -> Result<(), asset_system::error::DataWriteError> { tokio::fs::write(path, data.as_bytes()) .await - .map_err(|e| asset_system::error::DataWriteError::IoError(e.into())) + .map_err(asset_system::error::DataWriteError::IoError) } fn test_data() -> SheetData { diff --git a/systems/sheet/src/sheet/v1/constants.rs b/systems/sheet/src/sheet/v1/constants.rs index 7073278..eec7b5c 100644 --- a/systems/sheet/src/sheet/v1/constants.rs +++ b/systems/sheet/src/sheet/v1/constants.rs @@ -7,8 +7,7 @@ // [OFFSET_INDEX_TABLE: u32] pub const CURRENT_SHEET_VERSION: u8 = 1; -pub const HEADER_SIZE: usize = 0 - + 1 // SHEET_VERSION +pub const HEADER_SIZE: usize = 1 // SHEET_VERSION + 2 // MAPPING_BUCKET_COUNT + 4 // INDEX_COUNT + 4 // OFFSET_MAPPING_DIR @@ -21,8 +20,7 @@ pub const HEADER_SIZE: usize = 0 // [BUCKET_OFFSET: u32] // [BUCKET_LENGTH: u32] -pub const MAPPING_DIR_ENTRY_SIZE: usize = 0 - + 4 // BUCKET_HASH_PREFIX +pub const MAPPING_DIR_ENTRY_SIZE: usize = 4 // BUCKET_HASH_PREFIX + 4 // BUCKET_OFFSET + 4 // BUCKET_LENGTH ; @@ -36,8 +34,7 @@ pub const MAPPING_DIR_ENTRY_SIZE: usize = 0 // [FORWARD_INFO_BYTES: ?] // [INDEX_OFFSET: u32] -pub const MAPPING_BUCKET_MIN_SIZE: usize = 0 - + 1 // KEY_LEN +pub const MAPPING_BUCKET_MIN_SIZE: usize = 1 // KEY_LEN + 1 // FORWARD_TYPE + 1 // FORWARD_INFO_LEN + 2 // KEY_BYTES (MIN:1) + FORWARD_INFO_BYTES (MIN:1) @@ -51,8 +48,7 @@ pub const MAPPING_BUCKET_MIN_SIZE: usize = 0 // [REMOTE_FLAG: u8] // [RESERVED: u8; 3] -pub const INDEX_ENTRY_SIZE: usize = 0 - + 4 // INDEX_ID +pub const INDEX_ENTRY_SIZE: usize = 4 // INDEX_ID + 2 // INDEX_VERSION + 1 // REMOTE_FLAG + 3 // RESERVED diff --git a/systems/sheet/src/sheet/v1/writer.rs b/systems/sheet/src/sheet/v1/writer.rs index 4c580b2..fdc4caa 100644 --- a/systems/sheet/src/sheet/v1/writer.rs +++ b/systems/sheet/src/sheet/v1/writer.rs @@ -18,9 +18,9 @@ pub fn convert_sheet_data_to_bytes(sheet_data: SheetData) -> Vec { for mapping in &mappings { let source = mapping.index_source(); let key = (source.is_remote(), source.id(), source.version()); - if !source_to_offset.contains_key(&key) { + if let std::collections::hash_map::Entry::Vacant(e) = source_to_offset.entry(key) { let offset = index_sources.len() as u32; - source_to_offset.insert(key, offset); + e.insert(offset); index_sources.push(IndexSource::new( source.is_remote(), source.id(), @@ -36,10 +36,7 @@ pub fn convert_sheet_data_to_bytes(sheet_data: SheetData) -> Vec { for mapping in mappings { let hash = calculate_path_hash(mapping.value()); let bucket_key = hash >> 16; // Take high 16 bits as bucket key - buckets - .entry(bucket_key) - .or_insert_with(Vec::new) - .push(mapping); + buckets.entry(bucket_key).or_default().push(mapping); } let bucket_count = buckets.len() as u16; -- cgit