Skip to content

Commit 81ef80d

Browse files
committed
feat: adding utilities for arrays and introducing MemoryViewDereferenceable trait
1 parent 539b87a commit 81ef80d

File tree

14 files changed

+313
-256
lines changed

14 files changed

+313
-256
lines changed

examples/dyn_memory/src/main.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::{
22
self,
33
error::Error,
4-
marker,
54
};
65

76
use raw_struct::{
87
raw_struct,
98
Copy,
9+
CopyConstructable,
1010
CopyMemory,
11-
FromMemoryView,
1211
ViewableSized,
1312
};
1413

@@ -20,14 +19,23 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2019
"Memory size: 0x{:X}",
2120
<Container::<u64> as ViewableSized>::memory_size()
2221
);
23-
println!("Vat a = 0x{:X}", object.var_a()?);
24-
println!("Inner = 0x{:X}", object.inner()?);
25-
println!("Vat b = 0x{:X}", object.var_b()?);
22+
println!(
23+
"Vat a = 0x{:X}",
24+
object.read_field(Container::<u64>::var_a)?
25+
);
26+
println!(
27+
"Inner = 0x{:X}",
28+
object.read_field(Container::<u64>::inner)?
29+
);
30+
println!(
31+
"Vat b = 0x{:X}",
32+
object.read_field(Container::<u64>::var_b)?
33+
);
2634
Ok(())
2735
}
2836

2937
#[raw_struct(memory = "([u8; 0x10], T)")]
30-
struct Container<T: marker::Copy + FromMemoryView> {
38+
struct Container<T: CopyConstructable + 'static> {
3139
#[field(offset = 0x00)]
3240
pub var_a: u64,
3341

examples/minimal/src/main.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use raw_struct::{
88
raw_struct,
99
Copy,
1010
FromMemoryView,
11+
MemoryView,
12+
MemoryViewDereferenceable,
1113
Reference,
1214
ViewableSized,
1315
};
@@ -26,18 +28,24 @@ fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
2628

2729
{
2830
let object = Reference::<MyStruct, _>::new(memory.as_slice(), 0x00);
29-
println!("field_a = {:X}", object.field_a()?);
30-
println!("field_b = {:X}", object.field_b()?);
31+
println!("field_a = {:X}", object.read_field(MyStruct::field_a)?);
32+
println!("field_b = {:X}", object.read_field(MyStruct::field_b)?);
3133

3234
let object = object.cast::<MyStructExt>();
33-
println!("ext_field_a = {:X}", object.ext_field_a()?);
34-
println!("field_base = {:X}", object.field_base()?);
35+
println!(
36+
"ext_field_a = {:X}",
37+
object.read_field(MyStructExt::ext_field_a)?
38+
);
39+
println!(
40+
"field_base = {:X}",
41+
object.read_field(MyStructExt::ext_field_a)?
42+
);
3543
}
3644

3745
{
3846
let object = Copy::<MyStruct>::read_from_memory(&memory.as_slice(), 0x00)?;
39-
println!("field_a = {:X}", object.field_a()?);
40-
println!("field_b = {:X}", object.field_b()?);
47+
println!("field_a = {:X}", object.read_field(MyStruct::field_a)?);
48+
println!("field_b = {:X}", object.read_field(MyStruct::field_b)?);
4149
}
4250

4351
Ok(())
@@ -56,21 +64,20 @@ struct MyStruct {
5664
#[field(offset = 0x04)]
5765
pub field_b: u32,
5866

59-
/// Showcasing the custom getter name
60-
#[field(offset = 0x08, getter = "get_field_c")]
67+
#[field(offset = 0x08)]
6168
pub field_c: [u8; 0x8],
6269

6370
/// Sized array of other raw_structs
6471
#[field(offset = 0x10)]
65-
pub field_d: Ptr64<[Copy<MyArrayElement>; 0x20]>,
72+
pub field_d: Ptr64<[MyArrayElement; 0x20]>,
6673

6774
/// Array to another copyable
6875
#[field(offset = 0x10)]
6976
pub field_e: Ptr64<[u8]>,
7077

7178
/// Advanced array to other raw_structs
7279
#[field(offset = 0x18)]
73-
pub field_f: Ptr64<[Copy<MyStruct>]>,
80+
pub field_f: Ptr64<[MyStruct]>,
7481

7582
/// Advanced array to other raw_structs
7683
#[field(offset = 0x18)]
@@ -81,7 +88,7 @@ struct MyStruct {
8188
}
8289

8390
#[raw_struct]
84-
struct MyStructBase<T: FromMemoryView> {
91+
struct MyStructBase<T: FromMemoryView + 'static> {
8592
#[field(offset = 0x00)]
8693
pub field_base: T,
8794
}

raw_struct/src/builtins/array.rs

Lines changed: 0 additions & 122 deletions
This file was deleted.

raw_struct/src/builtins/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
1-
// mod array;
2-
// pub use array::{
3-
// Array,
4-
// SizedArray,
5-
// };
6-
7-
// mod ptr;
8-
// pub use ptr::Ptr64;
1+
mod ptr;
2+
pub use ptr::Ptr64;

raw_struct/src/builtins/ptr.rs

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
use core::{
2-
marker::{
3-
self,
4-
PhantomData,
5-
},
6-
ops::Deref,
1+
use core::marker::{
2+
self,
3+
PhantomData,
74
};
85

96
use crate::{
10-
builtins::Array,
117
Copy,
128
CopyConstructable,
139
FromMemoryView,
1410
MemoryDecodeError,
1511
MemoryView,
1612
Reference,
17-
SizedViewable,
1813
Viewable,
14+
ViewableSized,
1915
};
2016

2117
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
2218
pub struct Ptr64<T: ?Sized> {
23-
pub address: u64,
19+
address: u64,
2420
_type: PhantomData<T>,
2521
}
2622

@@ -29,19 +25,24 @@ impl<T: ?Sized> Clone for Ptr64<T> {
2925
*self
3026
}
3127
}
28+
3229
impl<T: ?Sized> marker::Copy for Ptr64<T> {}
3330

3431
impl<T: ?Sized> CopyConstructable for Ptr64<T> {}
3532

3633
impl<T: ?Sized> Ptr64<T> {
37-
pub fn is_null(&self) -> bool {
34+
pub const fn address(&self) -> u64 {
35+
self.address
36+
}
37+
38+
pub const fn is_null(&self) -> bool {
3839
self.address == 0
3940
}
4041

41-
pub fn cast<V>(&self) -> Ptr64<V> {
42+
pub const fn cast<V>(&self) -> Ptr64<V> {
4243
Ptr64::<V> {
4344
address: self.address,
44-
_type: Default::default(),
45+
_type: PhantomData {},
4546
}
4647
}
4748
}
@@ -61,53 +62,17 @@ impl<T: FromMemoryView> Ptr64<T> {
6162

6263
impl<T: Viewable> Ptr64<T> {
6364
#[must_use]
64-
pub fn value_reference<M: MemoryView>(&self, memory: M) -> Option<Reference<T, M>> {
65+
pub fn reference_value<M: MemoryView>(&self, memory: M) -> Option<Reference<T, M>> {
6566
(self.address > 0).then(|| Reference::new(memory, self.address))
6667
}
6768
}
6869

69-
impl<T: SizedViewable> Ptr64<T> {
70+
impl<T: ViewableSized> Ptr64<T> {
7071
/// Create a copy of the value the pointer points to
7172
#[must_use = "copied result must be used"]
72-
pub fn value_copy<M: MemoryView>(&self, memory: &M) -> Result<Option<Copy<T>>, M::AccessError> {
73+
pub fn copy_value<M: MemoryView>(&self, memory: &M) -> Result<Option<Copy<T>>, M::AccessError> {
7374
(self.address > 0)
7475
.then(|| Copy::<T>::read_from_memory(memory, self.address))
7576
.transpose()
7677
}
7778
}
78-
79-
impl<T> Array<T> for Ptr64<[T]> {
80-
fn start_address(&self) -> u64 {
81-
self.address
82-
}
83-
84-
fn len(&self) -> Option<usize> {
85-
None
86-
}
87-
}
88-
89-
impl<T: 'static> Deref for Ptr64<[T]> {
90-
type Target = dyn Array<T>;
91-
92-
fn deref(&self) -> &Self::Target {
93-
self
94-
}
95-
}
96-
97-
impl<T, const N: usize> Array<T> for Ptr64<[T; N]> {
98-
fn start_address(&self) -> u64 {
99-
self.address
100-
}
101-
102-
fn len(&self) -> Option<usize> {
103-
Some(N)
104-
}
105-
}
106-
107-
impl<T: 'static, const N: usize> Deref for Ptr64<[T; N]> {
108-
type Target = dyn Array<T>;
109-
110-
fn deref(&self) -> &Self::Target {
111-
self
112-
}
113-
}

0 commit comments

Comments
 (0)