Skip to content

Commit af6191d

Browse files
committed
feat: adding Clone to Reference
1 parent 6b9bc41 commit af6191d

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

raw_struct/src/error.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ use alloc::{
22
borrow::Cow,
33
format,
44
};
5-
use core::{
6-
convert::Infallible,
7-
fmt::{
8-
self,
9-
Debug,
10-
},
5+
use core::fmt::{
6+
self,
7+
Debug,
118
};
129

1310
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
@@ -41,7 +38,7 @@ impl core::error::Error for AccessViolation {}
4138
impl std::error::Error for AccessViolation {}
4239

4340
#[derive(Debug)]
44-
pub struct AccessError<S = Infallible> {
41+
pub struct AccessError<S> {
4542
pub source: S,
4643

4744
pub offset: u64,

raw_struct/src/memory.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ use core::{
1010

1111
use crate::error::AccessViolation;
1212

13-
pub trait MemoryView: Send + Sync {
14-
type Error;
15-
16-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error>;
13+
pub trait MemoryView<E> {
14+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), E>;
1715
}
1816

19-
impl MemoryView for &[u8] {
20-
type Error = AccessViolation;
21-
22-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error> {
17+
impl MemoryView<AccessViolation> for &[u8] {
18+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), AccessViolation> {
2319
let offset = offset as usize;
2420
if offset + buffer.len() > self.len() {
2521
return Err(AccessViolation);
@@ -41,7 +37,7 @@ impl<T: marker::Copy> CopyMemoryView<T> {
4137
}
4238
}
4339

44-
impl<T: marker::Copy + Send + Sync> MemoryView for CopyMemoryView<T> {
40+
impl<T: marker::Copy> MemoryView for CopyMemoryView<T> {
4541
type Error = AccessViolation;
4642

4743
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error> {
@@ -56,7 +52,7 @@ impl<T: marker::Copy + Send + Sync> MemoryView for CopyMemoryView<T> {
5652
}
5753
}
5854

59-
impl<T: marker::Copy + Send + Sync> From<T> for CopyMemoryView<T> {
55+
impl<T: marker::Copy> From<T> for CopyMemoryView<T> {
6056
fn from(value: T) -> Self {
6157
Self::new(value)
6258
}

raw_struct/src/reference.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,41 @@ use crate::{
2121

2222
pub struct ReferenceMemory<MemoryError> {
2323
address: u64,
24-
inner: Arc<dyn MemoryView<Error = MemoryError>>,
24+
inner: Arc<dyn MemoryView<MemoryError>>,
2525
}
2626

2727
impl<MemoryError> ReferenceMemory<MemoryError> {
2828
pub fn address(&self) -> u64 {
2929
self.address
3030
}
3131

32-
pub fn memory_view(&self) -> &Arc<dyn MemoryView<Error = MemoryError>> {
32+
pub fn memory_view(&self) -> &Arc<dyn MemoryView<MemoryError>> {
3333
&self.inner
3434
}
3535
}
3636

37-
impl<MemoryError> MemoryView for ReferenceMemory<MemoryError> {
38-
type Error = MemoryError;
39-
40-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error> {
37+
impl<MemoryError> MemoryView<MemoryError> for ReferenceMemory<MemoryError> {
38+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), MemoryError> {
4139
self.inner.read_memory(self.address + offset, buffer)
4240
}
4341
}
4442

43+
impl<MemoryError> Clone for ReferenceMemory<MemoryError> {
44+
fn clone(&self) -> Self {
45+
Self {
46+
address: self.address,
47+
inner: self.inner.clone(),
48+
}
49+
}
50+
}
51+
4552
/// A reference to an object living in the underlying memory view.
4653
pub struct Reference<T: ?Sized + Viewable, MemoryError: 'static> {
4754
inner: T::Instance<ReferenceMemory<MemoryError>>,
4855
}
4956

5057
impl<T: ?Sized + Viewable, MemoryError: 'static> Reference<T, MemoryError> {
51-
pub fn new(memory: Arc<dyn MemoryView<Error = MemoryError>>, address: u64) -> Self {
58+
pub fn new(memory: Arc<dyn MemoryView<MemoryError>>, address: u64) -> Self {
5259
Self {
5360
inner: T::create_view(ReferenceMemory {
5461
address,
@@ -62,7 +69,7 @@ impl<T: ?Sized + Viewable, MemoryError: 'static> Reference<T, MemoryError> {
6269
memory.address
6370
}
6471

65-
pub fn base_memory(&self) -> &Arc<dyn MemoryView<Error = MemoryError>> {
72+
pub fn base_memory(&self) -> &Arc<dyn MemoryView<MemoryError>> {
6673
let memory = self.inner.object_memory();
6774
&memory.inner
6875
}
@@ -98,3 +105,14 @@ impl<T: Viewable + ?Sized, MemoryError> Deref for Reference<T, MemoryError> {
98105
self.inner.get_accessor()
99106
}
100107
}
108+
109+
impl<T: Viewable + ?Sized, MemoryError> Clone for Reference<T, MemoryError>
110+
where
111+
T::Instance<ReferenceMemory<MemoryError>>: Clone,
112+
{
113+
fn clone(&self) -> Self {
114+
Self {
115+
inner: self.inner.clone(),
116+
}
117+
}
118+
}

raw_struct/src/view.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use alloc::borrow::Cow;
22

33
use crate::memory::MemoryView;
44

5-
pub trait ViewableBase<M: MemoryView>: Send + Sync {
5+
pub trait ViewableBase<M: MemoryView> {
66
fn object_memory(&self) -> &M;
77
}
88

@@ -27,7 +27,7 @@ pub trait Viewable: 'static {
2727
/// Stronger type of viewable which indicates that the object can be copied
2828
pub trait Copyable: Viewable {
2929
/// Const memory used for copying the value
30-
type Memory: Copy + Send + Sync;
30+
type Memory: Copy;
3131

3232
/// Byte size of the copy memory
3333
const MEMORY_SIZE: usize = core::mem::size_of::<Self::Memory>();

raw_struct_derive/src/derive_raw_struct.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ impl<'a> ViewableGenerator<'a> {
379379
}
380380

381381
fn generate_instance_struct(&self) -> TokenStream {
382+
let vis = &self.source.vis;
382383
let instance_name = &self.instance_name;
383384
let accessor_name = &self.accessor_name;
384385

@@ -387,12 +388,12 @@ impl<'a> ViewableGenerator<'a> {
387388

388389
quote! {
389390
#[allow(non_camel_case_types)]
390-
struct #instance_name <A: ?Sized, M: ::raw_struct::MemoryView> {
391+
#vis struct #instance_name <A: ?Sized, M: ::raw_struct::MemoryView> {
391392
_accessor: ::core::marker::PhantomData<A>,
392393
memory: M,
393394
}
394395

395-
impl<A: ?Sized + Send + Sync, M: ::raw_struct::MemoryView> ::raw_struct::ViewableBase<M> for #instance_name<A, M> {
396+
impl<A: ?Sized, M: ::raw_struct::MemoryView> ::raw_struct::ViewableBase<M> for #instance_name<A, M> {
396397
fn object_memory(&self) -> &M {
397398
&self.memory
398399
}

0 commit comments

Comments
 (0)