Skip to content

Commit

Permalink
Changed: Removed some remaining core::fmt references reducing size
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Dec 30, 2023
1 parent 70eae8d commit e03d46d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ memoffset = "0.9.0"
dirs = "5.0.1"
errno = "0.3.3"
spin = "0.9.8"
itoa = "1.0.10"

[dev-dependencies]
rstest = "0.18.1"
Expand Down
3 changes: 2 additions & 1 deletion src-rust/src/internal/locator_header_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ impl LocatorHeaderFinder {
// no_std
let mut name = String::from("/Reloaded.Memory.Buffers.MemoryBuffer, PID ");
let sys_info = get_sys_info();
name.push_str(&sys_info.this_process_id.to_string());
let mut buffer = itoa::Buffer::new();
name.push_str(buffer.format(sys_info.this_process_id));

#[cfg(target_os = "windows")]
return Box::new(WindowsMemoryMappedFile::new(
Expand Down
3 changes: 1 addition & 2 deletions src-rust/src/internal/memory_mapped_file_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ extern crate alloc;

use alloc::ffi::CString;
use alloc::string::String;
use alloc::string::ToString;
use core::mem::MaybeUninit;
use core::ptr::null_mut;

Expand Down Expand Up @@ -43,7 +42,7 @@ impl UnixMemoryMappedFile {
new_name.push_str(BASE_DIR);
new_name.push_str(name);

let file_name = CString::new(new_name.to_string()).expect("CString::new failed");
let file_name = CString::new(new_name.clone()).expect("CString::new failed");

let mut file_descriptor = unsafe { open(file_name.as_ptr(), O_RDWR) };
let already_existed = file_descriptor != -1;
Expand Down
21 changes: 21 additions & 0 deletions src-rust/src/structs/errors/buffer_allocation_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,27 @@ pub struct BufferAllocationError {
pub text: &'static str,
}

#[allow(clippy::inherent_to_string_shadow_display)]
impl BufferAllocationError {
pub fn to_string(&self) -> String {
// We save some space here for C binding use.
#[cfg(feature = "no_format")]
{
let mut error_message = String::from("Buffer Allocation Error: ");
error_message.push_str(self.text);
error_message
}

#[cfg(not(feature = "no_format"))]
{
format!(
"Buffer Allocation Error: {}. Settings: {:?}",
self.text, self.settings
)
}
}
}

impl core::fmt::Display for BufferAllocationError {
#[cfg_attr(feature = "size_opt", optimize(size))]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down
20 changes: 20 additions & 0 deletions src-rust/src/structs/errors/buffer_search_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@ pub struct BufferSearchError {
pub text: &'static str,
}

#[allow(clippy::inherent_to_string_shadow_display)]
impl BufferSearchError {
pub fn to_string(&self) -> String {
#[cfg(feature = "no_format")]
{
let mut error_message = String::from("Buffer Search Error: ");
error_message.push_str(self.text);
error_message
}

#[cfg(not(feature = "no_format"))]
{
format!(
"Buffer Search Error: {}. Settings: {:?}",
self.text, self.settings
)
}
}
}

impl Display for BufferSearchError {
#[cfg_attr(feature = "size_opt", optimize(size))]
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
Expand Down
3 changes: 2 additions & 1 deletion src-rust/src/utilities/linux_map_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ fn parse_memory_map_from_process_id(process_id: i32) -> Vec<MemoryMapEntry> {
// Construct the path to the maps file for the given process ID.
// no std!!
let mut maps_path = String::from("/proc/");
maps_path.push_str(&process_id.to_string());
let mut buffer = itoa::Buffer::new();
maps_path.push_str(buffer.format(process_id));
maps_path.push_str("/maps");

// Read all the lines from the file into a single string.
Expand Down

0 comments on commit e03d46d

Please sign in to comment.