Skip to content

Commit b1ec7c8

Browse files
committed
feat: adding no_std and no_alloc support
1 parent e6d12e2 commit b1ec7c8

File tree

5 files changed

+45
-24
lines changed

5 files changed

+45
-24
lines changed

raw_struct/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ readme = "../README.MD"
1313
raw_struct_derive = { version = "*", path = "../raw_struct_derive" }
1414

1515
[features]
16-
no_std = []
16+
default = ["std"]
17+
std = ["alloc"]
18+
alloc = []

raw_struct/src/builtins/array.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use alloc::vec::Vec;
21
use core::{
32
self,
43
mem,
5-
ops::Range,
6-
slice,
74
};
85

96
use crate::{
@@ -35,16 +32,17 @@ impl<T: FromMemoryView> dyn Array<T> {
3532
}
3633

3734
impl<T: CopyConstructable> dyn Array<T> {
35+
#[cfg(feature = "alloc")]
3836
pub fn elements<M: MemoryView>(
3937
&self,
4038
memory: &M,
41-
range: Range<usize>,
42-
) -> Result<Vec<T>, M::AccessError> {
39+
range: crate::Range<usize>,
40+
) -> Result<alloc::vec::Vec<T>, M::AccessError> {
4341
let element_count = range.end - range.start;
44-
let mut result = Vec::with_capacity(element_count);
42+
let mut result = alloc::vec::Vec::with_capacity(element_count);
4543

4644
let result_buffer = unsafe {
47-
slice::from_raw_parts_mut(
45+
core::slice::from_raw_parts_mut(
4846
result.as_mut_ptr() as *mut u8,
4947
element_count * mem::size_of::<T>(),
5048
)
@@ -64,12 +62,13 @@ impl<T: SizedViewable> dyn Array<T> {
6462
Reference::new(memory, self.start_address() + offset)
6563
}
6664

65+
#[cfg(feature = "alloc")]
6766
pub fn elements_reference<M: MemoryView + Clone>(
6867
&self,
6968
memory: M,
70-
range: Range<usize>,
71-
) -> Vec<Reference<T, M>> {
72-
Vec::from_iter(range.map(|index| {
69+
range: core::range::legacy::Range<usize>,
70+
) -> alloc::vec::Vec<Reference<T, M>> {
71+
alloc::vec::Vec::from_iter(range.map(|index| {
7372
Reference::new(
7473
memory.clone(),
7574
self.start_address() + (index * T::memory_size()) as u64,
@@ -86,16 +85,17 @@ impl<T: SizedViewable> dyn Array<T> {
8685
Copy::read_from_memory(memory, self.start_address() + offset)
8786
}
8887

88+
#[cfg(feature = "alloc")]
8989
pub fn elements_copy<M: MemoryView>(
9090
&self,
9191
memory: &M,
92-
range: Range<usize>,
93-
) -> Result<Vec<Copy<T>>, M::AccessError> {
92+
range: crate::Range<usize>,
93+
) -> Result<alloc::vec::Vec<Copy<T>>, M::AccessError> {
9494
let element_count = range.end - range.start;
95-
let mut result = Vec::<T::Memory>::with_capacity(element_count);
95+
let mut result = alloc::vec::Vec::<T::Memory>::with_capacity(element_count);
9696

9797
unsafe {
98-
let buffer = slice::from_raw_parts_mut(
98+
let buffer = core::slice::from_raw_parts_mut(
9999
result.as_mut_ptr() as *mut u8,
100100
element_count * T::memory_size(),
101101
);
@@ -104,7 +104,10 @@ impl<T: SizedViewable> dyn Array<T> {
104104
result.set_len(element_count);
105105
};
106106

107-
Ok(result.into_iter().map(Copy::<T>::new).collect::<Vec<_>>())
107+
Ok(result
108+
.into_iter()
109+
.map(Copy::<T>::new)
110+
.collect::<alloc::vec::Vec<_>>())
108111
}
109112
}
110113

raw_struct/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ impl fmt::Display for OutOfBoundsViolation {
1818
}
1919
}
2020

21-
#[cfg(not(feature = "no_std"))]
21+
#[cfg(feature = "std")]
2222
impl std::error::Error for OutOfBoundsViolation {}
2323

24-
#[cfg(feature = "no_std")]
24+
#[cfg(not(feature = "std"))]
2525
impl core::error::Error for OutOfBoundsViolation {}
2626

2727
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
@@ -39,8 +39,8 @@ impl<A: Display, V: Display> fmt::Display for MemoryDecodeError<A, V> {
3939
}
4040
}
4141

42-
#[cfg(not(feature = "no_std"))]
42+
#[cfg(feature = "std")]
4343
impl<A: Display + Debug, V: Display + Debug> std::error::Error for MemoryDecodeError<A, V> {}
4444

45-
#[cfg(feature = "no_std")]
45+
#[cfg(not(feature = "std"))]
4646
impl<A: Display + Debug, V: Display + Debug> core::error::Error for MemoryDecodeError<A, V> {}

raw_struct/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
#![cfg_attr(feature = "no_std", no_std)]
1+
#![feature(new_range_api)]
2+
#![cfg_attr(not(feature = "std"), no_std)]
3+
4+
#[cfg(feature = "alloc")]
5+
extern crate alloc;
6+
7+
#[cfg(all(feature = "alloc", not(feature = "std")))]
8+
pub(crate) use core::range::Range;
9+
#[cfg(all(feature = "std"))]
10+
pub(crate) use std::range::Range;
211

312
pub mod builtins;
413

@@ -29,11 +38,9 @@ pub use reference::{
2938
};
3039

3140
mod copy;
41+
3242
pub use copy::{
3343
Copy,
3444
CopyMemory,
3545
};
3646
pub use raw_struct_derive::raw_struct;
37-
38-
extern crate alloc;
39-
pub use alloc::borrow::Cow;

raw_struct/src/memory.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ impl<M: MemoryView> MemoryView for &M {
2727
}
2828
}
2929

30+
#[cfg(feature = "alloc")]
31+
impl<M: ?Sized + MemoryView> MemoryView for alloc::sync::Arc<M> {
32+
type AccessError = M::AccessError;
33+
34+
fn read_memory(&self, offset: u64, buffer: &mut [u8]) -> Result<(), Self::AccessError> {
35+
M::read_memory(self, offset, buffer)
36+
}
37+
}
38+
3039
impl MemoryView for &[u8] {
3140
type AccessError = OutOfBoundsViolation;
3241

0 commit comments

Comments
 (0)