Skip to content

Commit

Permalink
Remove std feature so only one feature
Browse files Browse the repository at this point in the history
Features shouldn't be negative, but there's currently no way of
disabling dependencies so for this use case I believe `no_std` is
appropriate.

In future, when Cargo supports something like:
```
[features]
std = ["!dep:portable-atomic", "!dep:spin"]
```
, `no_std` feature shall be renamed to `std`.

Signed-off-by: Tin Svagelj <[email protected]>
  • Loading branch information
Caellian committed Sep 15, 2023
1 parent 7a11487 commit fd4767d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 44 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions doc/crate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
24 changes: 12 additions & 12 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<PoisonError<MutexGuard<'_, *mut u8>>> for LockingError {
fn from(_: PoisonError<MutexGuard<'_, *mut u8>>) -> Self {
LockingError::Poisoned {
Expand All @@ -78,7 +78,7 @@ impl From<PoisonError<MutexGuard<'_, *mut u8>>> for LockingError {
}
}

#[cfg(feature = "std")]
#[cfg(not(feature = "no_std"))]
impl From<PoisonError<MutexGuard<'_, crate::tracker::AllocationTracker>>> for LockingError {
fn from(_: PoisonError<MutexGuard<'_, crate::tracker::AllocationTracker>>) -> Self {
LockingError::Poisoned {
Expand All @@ -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!(
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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"
);
Expand Down Expand Up @@ -312,7 +312,7 @@ impl<Impl: ImplDetails> ContiguousMemoryStorage<Impl> {
/// behavior.
/// ([_see details_](https://doc.rust-lang.org/nomicon/leaking.html))
pub fn forget(self) {
std::mem::forget(self);
core::mem::forget(self);
}
}

Expand Down Expand Up @@ -509,7 +509,7 @@ pub type ContiguousMemory = ContiguousMemoryStorage<ImplDefault>;
/// ```
pub type UnsafeContiguousMemory = ContiguousMemoryStorage<ImplUnsafe>;

#[cfg(all(test, feature = "std"))]
#[cfg(all(test, not(feature = "no_std")))]
mod test {
use super::prelude::*;

Expand Down
8 changes: 4 additions & 4 deletions src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ impl<T: ?Sized> SyncContiguousEntryRef<T> {
let inner: *mut ReferenceState<T, ImplConcurrent> = self.inner.as_ref()
as *const ReferenceState<T, ImplConcurrent>
as *mut ReferenceState<T, ImplConcurrent>;
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
}
}
Expand Down Expand Up @@ -494,8 +494,8 @@ impl<T: ?Sized> ContiguousEntryRef<T> {
let inner: *mut ReferenceState<T, ImplDefault> = self.inner.as_ref()
as *const ReferenceState<T, ImplDefault>
as *mut ReferenceState<T, ImplDefault>;
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
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
28 changes: 14 additions & 14 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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};
Expand All @@ -44,7 +44,7 @@ pub(crate) trait MutexTypesafe<T: ?Sized> {
source: LockSource,
) -> Result<MutexGuard<T>, crate::error::LockingError>;
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no_std"))]
impl<T: ?Sized> MutexTypesafe<T> for Mutex<T> {
fn lock_named(&self, source: LockSource) -> Result<MutexGuard<T>, crate::error::LockingError> {
match self.lock() {
Expand All @@ -63,18 +63,18 @@ impl<T: ?Sized> MutexTypesafe<T> for Mutex<T> {
}
}
}
#[cfg(not(feature = "std"))]
#[cfg(feature = "no_std")]
impl<T: ?Sized> MutexTypesafe<T> for Mutex<T> {
fn lock_named(&self, _source: LockSource) -> Result<MutexGuard<T>, LockingError> {
Ok(self.lock())
}
fn try_lock_named(
&self,
_source: LockSource,
source: LockSource,
) -> Result<MutexGuard<T>, crate::error::LockingError> {
match self.try_lock() {
Some(it) => Ok(it),
None => Err(LockingError::WouldBlock),
None => Err(LockingError::WouldBlock { source }),
}
}
}
Expand All @@ -85,7 +85,7 @@ pub(crate) trait RwLockTypesafe<T: ?Sized> {
fn write_named(&self, source: LockSource) -> Result<RwLockWriteGuard<T>, LockingError>;
fn try_write_named(&self, source: LockSource) -> Result<RwLockWriteGuard<T>, LockingError>;
}
#[cfg(feature = "std")]
#[cfg(not(feature = "no_std"))]
impl<T: ?Sized> RwLockTypesafe<T> for RwLock<T> {
fn read_named(&self, source: LockSource) -> Result<RwLockReadGuard<T>, LockingError> {
match self.read() {
Expand Down Expand Up @@ -117,27 +117,27 @@ impl<T: ?Sized> RwLockTypesafe<T> for RwLock<T> {
}
}
}
#[cfg(not(feature = "std"))]
#[cfg(feature = "no_std")]
impl<T: ?Sized> RwLockTypesafe<T> for RwLock<T> {
fn read_named(&self, _source: LockSource) -> Result<RwLockReadGuard<T>, LockingError> {
Ok(self.read())
}

fn try_read_named(&self, _source: LockSource) -> Result<RwLockReadGuard<T>, LockingError> {
fn try_read_named(&self, source: LockSource) -> Result<RwLockReadGuard<T>, LockingError> {
match self.try_read() {
Some(guard) => Ok(guard),
None => Err(LockingError::WouldBlock),
None => Err(LockingError::WouldBlock { source }),
}
}

fn write_named(&self, _source: LockSource) -> Result<RwLockWriteGuard<T>, LockingError> {
Ok(self.write())
}

fn try_write_named(&self, _source: LockSource) -> Result<RwLockWriteGuard<T>, LockingError> {
fn try_write_named(&self, source: LockSource) -> Result<RwLockWriteGuard<T>, LockingError> {
match self.try_write() {
Some(guard) => Ok(guard),
None => Err(LockingError::WouldBlock),
None => Err(LockingError::WouldBlock { source }),
}
}
}
Expand Down

0 comments on commit fd4767d

Please sign in to comment.