perf: zero-alloc File → FileDto via into_parts() ownership transfer

- Add FileParts struct and File::into_parts() to yield owned fields
- Rewrite From<File> for FileDto to move id, name, path, folder_id,
  owner_id by ownership instead of .to_string() copies
- Eliminates ~5 heap allocations per file (5000 saved per 1000-file listing)
This commit is contained in:
Dionisio
2026-03-06 22:37:31 +01:00
parent d4a7cb0414
commit 55936699ce
2 changed files with 59 additions and 16 deletions
+37
View File
@@ -3,6 +3,24 @@ use crate::domain::services::path_service::StoragePath;
// Re-export entity errors from the centralized module
pub use super::entity_errors::{FileError, FileResult};
/// Owned parts of a [`File`] entity, produced by [`File::into_parts()`].
///
/// Consuming a `File` into `FileParts` **moves** every field without cloning,
/// eliminating 3-5 heap allocations that previously occurred when converting
/// `File → FileDto` via `.to_string()` on each getter.
pub struct FileParts {
pub id: String,
pub name: String,
pub storage_path: StoragePath,
pub path_string: String,
pub size: u64,
pub mime_type: String,
pub folder_id: Option<String>,
pub created_at: u64,
pub modified_at: u64,
pub owner_id: Option<String>,
}
/**
* Represents a file in the system's domain model.
*
@@ -167,6 +185,25 @@ impl File {
})
}
/// Consume the entity and return all fields by ownership.
///
/// Use this when converting `File` into a DTO to avoid cloning
/// every `String` field (saves 3-5 heap allocations per file).
pub fn into_parts(self) -> FileParts {
FileParts {
id: self.id,
name: self.name,
storage_path: self.storage_path,
path_string: self.path_string,
size: self.size,
mime_type: self.mime_type,
folder_id: self.folder_id,
created_at: self.created_at,
modified_at: self.modified_at,
owner_id: self.owner_id,
}
}
// Getters
pub fn id(&self) -> &str {
&self.id