diff --git a/Cargo.toml b/Cargo.toml index 54b25e6..6e6954c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,8 +19,7 @@ portable-atomic = { version = "1", default-features = false, optional = true } spin = { version = "0.9", optional = true } [features] -default = ["std", "ptr_metadata"] -std = [] +default = ["leak_data"] no_std = ["dep:portable-atomic", "dep:spin"] debug = [] leak_data = [] diff --git a/README.md b/README.md index 3a7edc1..10a10d8 100644 --- a/README.md +++ b/README.md @@ -64,8 +64,7 @@ contiguous_mem = { version = "0.4.*", default-features = false, features = ["no_ ### Features -- `std` (**default**) - use `std` environment sync primitives and locks -- `no_std` - enables `no_std` dependencies +- `no_std` - enables `no_std` dependencies for atomics, mutexes and rwlocks - `leak_data` (**default**) - disables `Copy` requirement for stored types, but any references in stored data will be leaked when the memory container is dropped - `debug` - enables `derive(Debug)` on structures unrelated to error handling diff --git a/doc/crate.md b/doc/crate.md index 46e1395..a7029de 100644 --- a/doc/crate.md +++ b/doc/crate.md @@ -15,8 +15,7 @@ See individual items for usage examples. ## Features -- `std` (**default**) - use `std` environment sync primitives and locks -- `no_std` - enables `no_std` dependencies +- `no_std` - enables `no_std` dependencies for atomics, mutexes and rwlocks - `leak_data` (**default**) - disables `Copy` requirement for stored types, but any references in stored data will be leaked when the memory container is dropped - `debug` - enables `derive(Debug)` on structures unrelated to error handling diff --git a/src/details.rs b/src/details.rs index fa3a4b1..feda2ce 100644 --- a/src/details.rs +++ b/src/details.rs @@ -17,9 +17,9 @@ use core::{ use core::marker::PhantomData; -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] use portable_atomic::{AtomicUsize, Ordering}; -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] use std::sync::atomic::{AtomicUsize, Ordering}; use crate::{ diff --git a/src/error.rs b/src/error.rs index 7c7f76f..5de0088 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,17 +2,17 @@ #[cfg(all(feature = "error_in_core"))] use core::error::Error; -#[cfg(all(not(feature = "error_in_core"), feature = "std"))] +#[cfg(all(not(feature = "error_in_core"), not(feature = "no_std")))] use std::error::Error; -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] use std::sync::MutexGuard; -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] use std::sync::PoisonError; use core::alloc::LayoutError; use core::fmt::Debug; -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] use core::fmt::{Display, Formatter, Result as FmtResult}; use crate::range::ByteRange; @@ -33,7 +33,7 @@ pub enum LockingError { }, } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Display for LockingError { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self { @@ -62,14 +62,14 @@ impl Display for LockingError { } } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Error for LockingError { fn source(&self) -> Option<&(dyn Error + 'static)> { None } } -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] impl From>> for LockingError { fn from(_: PoisonError>) -> Self { LockingError::Poisoned { @@ -78,7 +78,7 @@ impl From>> for LockingError { } } -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] impl From>> for LockingError { fn from(_: PoisonError>) -> Self { LockingError::Poisoned { @@ -95,7 +95,7 @@ pub struct RegionBorrowedError { pub range: ByteRange, } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Display for RegionBorrowedError { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { write!( @@ -106,7 +106,7 @@ impl Display for RegionBorrowedError { } } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Error for RegionBorrowedError { fn source(&self) -> Option<&(dyn Error + 'static)> { None @@ -164,7 +164,7 @@ pub enum LockSource { Reference, } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Display for ContiguousMemoryError { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { match self { @@ -199,7 +199,7 @@ impl Display for ContiguousMemoryError { } } -#[cfg(any(feature = "std", feature = "error_in_core"))] +#[cfg(any(not(feature = "no_std"), feature = "error_in_core"))] impl Error for ContiguousMemoryError { fn source(&self) -> Option<&(dyn Error + 'static)> { match self { diff --git a/src/lib.rs b/src/lib.rs index 40bd0cf..8f96370 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ #![allow(incomplete_features)] -#![cfg_attr(not(feature = "std"), no_std)] +#![cfg_attr(feature = "no_std", no_std)] #![cfg_attr( feature = "ptr_metadata", feature(ptr_metadata, unsize, specialization) @@ -9,10 +9,10 @@ #![warn(missing_docs)] #![doc = include_str!("../doc/crate.md")] -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] extern crate alloc; -#[cfg(all(not(feature = "std"), not(feature = "no_std")))] +#[cfg(all(feature = "no_std", not(feature = "no_std")))] compile_error!( "contiguous_mem: please enable 'no_std' feature to enable 'no_std' dependencies, or the default 'std' feature" ); @@ -312,7 +312,7 @@ impl ContiguousMemoryStorage { /// behavior. /// ([_see details_](https://doc.rust-lang.org/nomicon/leaking.html)) pub fn forget(self) { - std::mem::forget(self); + core::mem::forget(self); } } @@ -509,7 +509,7 @@ pub type ContiguousMemory = ContiguousMemoryStorage; /// ``` pub type UnsafeContiguousMemory = ContiguousMemoryStorage; -#[cfg(all(test, feature = "std"))] +#[cfg(all(test, not(feature = "no_std")))] mod test { use super::prelude::*; diff --git a/src/refs.rs b/src/refs.rs index fc738c4..304dbdf 100644 --- a/src/refs.rs +++ b/src/refs.rs @@ -265,8 +265,8 @@ impl SyncContiguousEntryRef { let inner: *mut ReferenceState = self.inner.as_ref() as *const ReferenceState as *mut ReferenceState; - std::ptr::drop_in_place(&mut (*inner).state); - std::mem::forget(self.inner); + core::ptr::drop_in_place(&mut (*inner).state); + core::mem::forget(self.inner); result } } @@ -494,8 +494,8 @@ impl ContiguousEntryRef { let inner: *mut ReferenceState = self.inner.as_ref() as *const ReferenceState as *mut ReferenceState; - std::ptr::drop_in_place(&mut (*inner).state); - std::mem::forget(self.inner); + core::ptr::drop_in_place(&mut (*inner).state); + core::mem::forget(self.inner); result } } diff --git a/src/tracker.rs b/src/tracker.rs index 617dbcf..e61390c 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -2,7 +2,7 @@ use core::alloc::Layout; -#[cfg(any(not(feature = "std")))] +#[cfg(any(feature = "no_std"))] use crate::types::Vec; use crate::{error::ContiguousMemoryError, range::ByteRange}; diff --git a/src/types.rs b/src/types.rs index b82adc4..4bd1db6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ //! Module re-exporting used types any polyfill to help with feature support. -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] mod std_imports { pub use std::rc::Rc; pub use std::sync::Arc; @@ -11,10 +11,10 @@ mod std_imports { pub use std::alloc as allocator; } -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] pub use std_imports::*; -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] mod nostd_imports { pub use spin::Mutex; pub use spin::MutexGuard; @@ -27,7 +27,7 @@ mod nostd_imports { pub use ::alloc::rc::Rc; pub use ::alloc::sync::Arc; } -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] pub use nostd_imports::*; use crate::error::{LockSource, LockingError}; @@ -44,7 +44,7 @@ pub(crate) trait MutexTypesafe { source: LockSource, ) -> Result, crate::error::LockingError>; } -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] impl MutexTypesafe for Mutex { fn lock_named(&self, source: LockSource) -> Result, crate::error::LockingError> { match self.lock() { @@ -63,18 +63,18 @@ impl MutexTypesafe for Mutex { } } } -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] impl MutexTypesafe for Mutex { fn lock_named(&self, _source: LockSource) -> Result, LockingError> { Ok(self.lock()) } fn try_lock_named( &self, - _source: LockSource, + source: LockSource, ) -> Result, crate::error::LockingError> { match self.try_lock() { Some(it) => Ok(it), - None => Err(LockingError::WouldBlock), + None => Err(LockingError::WouldBlock { source }), } } } @@ -85,7 +85,7 @@ pub(crate) trait RwLockTypesafe { fn write_named(&self, source: LockSource) -> Result, LockingError>; fn try_write_named(&self, source: LockSource) -> Result, LockingError>; } -#[cfg(feature = "std")] +#[cfg(not(feature = "no_std"))] impl RwLockTypesafe for RwLock { fn read_named(&self, source: LockSource) -> Result, LockingError> { match self.read() { @@ -117,16 +117,16 @@ impl RwLockTypesafe for RwLock { } } } -#[cfg(not(feature = "std"))] +#[cfg(feature = "no_std")] impl RwLockTypesafe for RwLock { fn read_named(&self, _source: LockSource) -> Result, LockingError> { Ok(self.read()) } - fn try_read_named(&self, _source: LockSource) -> Result, LockingError> { + fn try_read_named(&self, source: LockSource) -> Result, LockingError> { match self.try_read() { Some(guard) => Ok(guard), - None => Err(LockingError::WouldBlock), + None => Err(LockingError::WouldBlock { source }), } } @@ -134,10 +134,10 @@ impl RwLockTypesafe for RwLock { Ok(self.write()) } - fn try_write_named(&self, _source: LockSource) -> Result, LockingError> { + fn try_write_named(&self, source: LockSource) -> Result, LockingError> { match self.try_write() { Some(guard) => Ok(guard), - None => Err(LockingError::WouldBlock), + None => Err(LockingError::WouldBlock { source }), } } }