From ffd932c86efd05307e25dc86d61880b3d94e2c57 Mon Sep 17 00:00:00 2001 From: Jaby Date: Sun, 16 Oct 2022 16:08:12 +0200 Subject: [PATCH] Improve code --- src/Tools/psxcdgen_ex/src/main.rs | 6 ++--- src/Tools/psxcdgen_ex/src/types/helper.rs | 7 +++-- src/Tools/psxcdgen_ex/src/types/layout.rs | 11 ++++---- src/Tools/psxcdgen_ex/src/types/mod.rs | 32 ++++++++++++++++------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Tools/psxcdgen_ex/src/main.rs b/src/Tools/psxcdgen_ex/src/main.rs index 52a7cfa6..7f9e9619 100644 --- a/src/Tools/psxcdgen_ex/src/main.rs +++ b/src/Tools/psxcdgen_ex/src/main.rs @@ -20,9 +20,9 @@ fn populate() -> Result { folder }; - desc.root.borrow_mut().add_dir(folder); - desc.root.borrow_mut().add_file(file); - desc.root.borrow_mut().add_file(file2); + desc.add_dir(folder); + desc.add_file(file); + desc.add_file(file2); desc.calculate_lbas(); Ok(desc) diff --git a/src/Tools/psxcdgen_ex/src/types/helper.rs b/src/Tools/psxcdgen_ex/src/types/helper.rs index 67bd8339..5295545e 100644 --- a/src/Tools/psxcdgen_ex/src/types/helper.rs +++ b/src/Tools/psxcdgen_ex/src/types/helper.rs @@ -1,5 +1,4 @@ use super::*; -use std::{cell::RefCell, rc::Rc}; const CURRENT_DIR_NAME:&'static str = "/x00"; const PARENT_DIR_NAME:&'static str = "/x01"; @@ -23,8 +22,8 @@ impl DirectoryRecordMember { } } -pub fn collect_path_table_member(root: Rc>) -> Vec { - fn collect_path_table_for(collection: &mut Vec, dirs: &Vec>>, parent_id: usize) { +pub fn collect_path_table_member(root: SharedPtr) -> Vec { + fn collect_path_table_for(collection: &mut Vec, dirs: &Vec>, parent_id: usize) { let mut cur_dirs = Vec::new(); for dir in dirs { @@ -49,7 +48,7 @@ pub fn collect_path_table_member(root: Rc>) -> Vec>>, dirs: &Vec>>) -> Vec { +pub fn collect_directory_record_member(files: &Vec>, dirs: &Vec>) -> Vec { let mut collection = Vec::new(); collection.push(DirectoryRecordMember::Directory{name: CURRENT_DIR_NAME.to_owned()}); diff --git a/src/Tools/psxcdgen_ex/src/types/layout.rs b/src/Tools/psxcdgen_ex/src/types/layout.rs index 51c629b5..987c17b9 100644 --- a/src/Tools/psxcdgen_ex/src/types/layout.rs +++ b/src/Tools/psxcdgen_ex/src/types/layout.rs @@ -1,5 +1,4 @@ use super::*; -use std::cell::RefCell; pub type MemoryLayout = Vec; pub type MemoryLayoutMut = Vec; @@ -29,14 +28,14 @@ macro_rules! declare_memory_layout { } pub enum [< Layout$($val:camel),* >] { - SystemArea(Rc>), - PVD(Rc>), + SystemArea(SharedPtr), + PVD(SharedPtr), PathTables, - Directory(Rc>), - File(Rc>) + Directory(SharedPtr), + File(SharedPtr) } - fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: Rc>) { + fn [< add_dir_and_subdir $(_$val),* >]<'a>(layout: &mut Vec<[< Layout$($val:camel),* >]>, dir: SharedPtr) { layout.push([< Layout$($val:camel),* >]::Directory(dir.clone())); let $($val),* dir = dir.[< borrow $(_$val),* >](); diff --git a/src/Tools/psxcdgen_ex/src/types/mod.rs b/src/Tools/psxcdgen_ex/src/types/mod.rs index 14ef8043..882704e7 100644 --- a/src/Tools/psxcdgen_ex/src/types/mod.rs +++ b/src/Tools/psxcdgen_ex/src/types/mod.rs @@ -6,10 +6,16 @@ use layout::LayoutMut; use tool_helper::Error; use std::{cell::RefCell, rc::Rc}; +pub type SharedPtr = Rc>; + +pub fn new_shared_ptr(value: T) -> SharedPtr { + Rc::new(RefCell::new(value)) +} + pub struct CDDesc { - system_area: Rc>, - pvd: Rc>, - pub root: Rc> + system_area: SharedPtr, + pvd: SharedPtr, + root: SharedPtr } impl CDDesc { @@ -17,7 +23,7 @@ impl CDDesc { pub fn new() -> CDDesc { match Directory::new("root") { - Ok(root) => CDDesc{system_area: Rc::new(RefCell::new(SystemArea::new())), pvd: Rc::new(RefCell::new(PrimaryVolumeDescriptor::new())), root: Rc::new(RefCell::new(root))}, + Ok(root) => CDDesc{system_area: new_shared_ptr(SystemArea::new()), pvd: new_shared_ptr(PrimaryVolumeDescriptor::new()), root: new_shared_ptr(root)}, Err(error) => panic!("Creating root directory failed with: {}", error) } } @@ -30,6 +36,14 @@ impl CDDesc { layout::DefaultLayoutMut::new(self) } + pub fn add_dir(&mut self, dir: Directory) { + self.root.borrow_mut().add_dir(dir); + } + + pub fn add_file(&mut self, file: File) { + self.root.borrow_mut().add_file(file); + } + pub fn calculate_lbas(&mut self) { let mut path_table_properties = { let mut size_bytes = 0; @@ -79,7 +93,7 @@ impl CDDesc { } } - fn for_each_dir_mut(dir: Rc>, function: &T) { + fn for_each_dir_mut(dir: SharedPtr, function: &T) { let mut dir = dir.borrow_mut(); function(&mut dir); @@ -116,8 +130,8 @@ impl PrimaryVolumeDescriptor { pub struct Directory { pub name: DirectoryName, pub properties: Properties, - files: Vec>>, - dirs: Vec>> + files: Vec>, + dirs: Vec> } impl Directory { @@ -126,11 +140,11 @@ impl Directory { } pub fn add_dir(&mut self, dir: Directory) { - self.dirs.push(Rc::new(RefCell::new(dir))); + self.dirs.push(new_shared_ptr(dir)); } pub fn add_file(&mut self, file: File) { - self.files.push(Rc::new(RefCell::new(file))); + self.files.push(new_shared_ptr(file)); } fn update_content_size(&mut self) {