Skip to content

Commit e14f103

Browse files
committed
feat: improved error type by requireing Sync + Send
1 parent 2cd8cf8 commit e14f103

File tree

5 files changed

+13
-18
lines changed

5 files changed

+13
-18
lines changed

examples/dyn_memory/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use raw_struct::{
1111
Viewable,
1212
};
1313

14-
fn main() -> Result<(), Box<dyn Error>> {
14+
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1515
let buffer = [0x1122u64, 0x8877, 0x9988];
1616
let object = Copy::<dyn Container<u64>>::read_object(&buffer, 0x00)?;
1717

examples/minimal/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use raw_struct::{
1717
Viewable,
1818
};
1919

20-
fn main() -> Result<(), Box<dyn Error>> {
20+
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2121
let mut memory = [0u8; 0x20];
2222
memory[0..4].copy_from_slice(&0x6Fu32.to_le_bytes());
2323
memory[4..8].copy_from_slice(&0x99u32.to_le_bytes());

raw_struct/src/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use core::fmt;
99
#[cfg(not(feature = "no_std"))]
1010
pub use std::error::Error as ErrorType;
1111

12+
pub type Error = Box<dyn ErrorType + Send + Sync + 'static>;
13+
1214
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
1315
pub enum AccessMode {
1416
Read,
@@ -37,7 +39,7 @@ impl ErrorType for AccessViolation {}
3739

3840
#[derive(Debug)]
3941
pub struct AccessError {
40-
pub source: Box<dyn ErrorType + 'static>,
42+
pub source: Error,
4143

4244
pub offset: u64,
4345
pub size: usize,

raw_struct/src/memory.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,16 @@ use core::{
88
};
99

1010
use crate::error::{
11-
self,
1211
AccessViolation,
12+
Error,
1313
};
1414

1515
pub trait MemoryView {
16-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Box<dyn error::ErrorType>>;
16+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Error>;
1717
}
1818

1919
impl<T: Copy> MemoryView for T {
20-
fn read_memory(
21-
&self,
22-
offset: u64,
23-
buffer: &mut [u8],
24-
) -> Result<(), alloc::boxed::Box<dyn error::ErrorType>> {
20+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Error> {
2521
let src_buffer = unsafe {
2622
core::slice::from_raw_parts(self as *const _ as *const u8, core::mem::size_of_val(self))
2723
};
@@ -37,12 +33,12 @@ impl<T: Copy> MemoryView for T {
3733
}
3834

3935
pub trait FromMemoryView: Sized {
40-
fn read_object(view: &dyn MemoryView, offset: u64) -> Result<Self, Box<dyn error::ErrorType>>;
36+
fn read_object(view: &dyn MemoryView, offset: u64) -> Result<Self, Error>;
4137
// fn read_boxed(view: &dyn MemoryView, offset: u64) -> Result<Box<Self>, Box<dyn error::ErrorType>>;
4238
}
4339

4440
impl<T: Copy> FromMemoryView for T {
45-
fn read_object(view: &dyn MemoryView, offset: u64) -> Result<Self, Box<dyn error::ErrorType>> {
41+
fn read_object(view: &dyn MemoryView, offset: u64) -> Result<Self, Error> {
4642
let mut result = MaybeUninit::uninit();
4743
let size = mem::size_of_val(&result);
4844

raw_struct/src/reference.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use alloc::{
2-
boxed::Box,
3-
sync::Arc,
4-
};
1+
use alloc::sync::Arc;
52
use core::{
63
self,
74
marker,
@@ -10,8 +7,8 @@ use core::{
107

118
use crate::{
129
error::{
13-
self,
1410
AccessError,
11+
Error,
1512
},
1613
memory::MemoryView,
1714
view::ViewableImplementation,
@@ -37,7 +34,7 @@ impl ReferenceMemory {
3734
}
3835

3936
impl MemoryView for ReferenceMemory {
40-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Box<dyn error::ErrorType>> {
37+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Error> {
4138
self.inner.read_memory(self.address + offset, buffer)
4239
}
4340
}

0 commit comments

Comments
 (0)