Skip to content

Commit 937c3b0

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

File tree

5 files changed

+34
-16
lines changed

5 files changed

+34
-16
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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use core::{
1010

1111
use crate::error::AccessViolation;
1212

13-
pub trait MemoryView: Send + Sync {
13+
pub trait MemoryView {
1414
type Error;
1515

1616
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error>;
@@ -19,7 +19,7 @@ pub trait MemoryView: Send + Sync {
1919
impl MemoryView for &[u8] {
2020
type Error = AccessViolation;
2121

22-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error> {
22+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), AccessViolation> {
2323
let offset = offset as usize;
2424
if offset + buffer.len() > self.len() {
2525
return Err(AccessViolation);
@@ -41,7 +41,7 @@ impl<T: marker::Copy> CopyMemoryView<T> {
4141
}
4242
}
4343

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

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

59-
impl<T: marker::Copy + Send + Sync> From<T> for CopyMemoryView<T> {
59+
impl<T: marker::Copy> From<T> for CopyMemoryView<T> {
6060
fn from(value: T) -> Self {
6161
Self::new(value)
6262
}

raw_struct/src/reference.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,20 @@ impl<MemoryError> ReferenceMemory<MemoryError> {
3737
impl<MemoryError> MemoryView for ReferenceMemory<MemoryError> {
3838
type Error = MemoryError;
3939

40-
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::Error> {
40+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), MemoryError> {
4141
self.inner.read_memory(self.address + offset, buffer)
4242
}
4343
}
4444

45+
impl<MemoryError> Clone for ReferenceMemory<MemoryError> {
46+
fn clone(&self) -> Self {
47+
Self {
48+
address: self.address,
49+
inner: self.inner.clone(),
50+
}
51+
}
52+
}
53+
4554
/// A reference to an object living in the underlying memory view.
4655
pub struct Reference<T: ?Sized + Viewable, MemoryError: 'static> {
4756
inner: T::Instance<ReferenceMemory<MemoryError>>,
@@ -98,3 +107,14 @@ impl<T: Viewable + ?Sized, MemoryError> Deref for Reference<T, MemoryError> {
98107
self.inner.get_accessor()
99108
}
100109
}
110+
111+
impl<T: Viewable + ?Sized, MemoryError> Clone for Reference<T, MemoryError>
112+
where
113+
T::Instance<ReferenceMemory<MemoryError>>: Clone,
114+
{
115+
fn clone(&self) -> Self {
116+
Self {
117+
inner: self.inner.clone(),
118+
}
119+
}
120+
}

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)