diff --git a/Cargo.toml b/Cargo.toml index c6b574b..3bb55bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,4 +12,9 @@ repository = "https://github.com/Kimundi/owning-ref-rs" keywords = ["reference", "sibling", "field", "owning"] [dependencies] -stable_deref_trait = "1.0.0" +stable_deref_trait = { version = "1.0.0", default-features = false } + +[features] +default = ["std"] +alloc = ["stable_deref_trait/alloc"] +std = ["stable_deref_trait/std"] diff --git a/src/lib.rs b/src/lib.rs index 7a29136..2417cab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![cfg_attr(feature = "alloc", feature(alloc))] +#![cfg_attr(not(feature = "std"), no_std)] #![warn(missing_docs)] /*! @@ -55,6 +57,7 @@ See the documentation around `OwningHandle` for more details. ## Basics ``` + extern crate owning_ref; use owning_ref::BoxRef; @@ -415,6 +418,7 @@ impl OwningRef { /// /// This can be used to safely erase the owner of any `OwningRef` /// to a `OwningRef, T>`. + #[cfg(any(feature = "alloc", feature = "std"))] pub fn map_owner_box(self) -> OwningRef, T> { OwningRef { reference: self.reference, @@ -659,6 +663,7 @@ impl OwningRefMut { /// /// This can be used to safely erase the owner of any `OwningRefMut` /// to a `OwningRefMut, T>`. + #[cfg(any(feature = "alloc", feature = "std"))] pub fn map_owner_box(self) -> OwningRefMut, T> { OwningRefMut { reference: self.reference, @@ -1077,10 +1082,18 @@ impl Hash for OwningRefMut where T: Hash { // std types integration and convenience type defs ///////////////////////////////////////////////////////////////////////////// +#[cfg(feature = "alloc")] use std::boxed::Box; +#[cfg(any(feature = "alloc", feature = "std"))] use std::rc::Rc; +#[cfg(feature = "alloc")] +use std::string::String; +#[cfg(any(feature = "alloc", feature = "std"))] use std::sync::Arc; +#[cfg(feature = "std")] use std::sync::{MutexGuard, RwLockReadGuard, RwLockWriteGuard}; +#[cfg(feature = "alloc")] +use std::vec::Vec; use std::cell::{Ref, RefCell, RefMut}; impl ToHandle for RefCell { @@ -1098,15 +1111,20 @@ impl ToHandleMut for RefCell { // what to do with error results. /// Typedef of a owning reference that uses a `Box` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type BoxRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Vec` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type VecRef = OwningRef, U>; /// Typedef of a owning reference that uses a `String` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type StringRef = OwningRef; /// Typedef of a owning reference that uses a `Rc` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type RcRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Arc` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type ArcRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Ref` as the owner. @@ -1114,38 +1132,51 @@ pub type RefRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `RefMut` as the owner. pub type RefMutRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `MutexGuard` as the owner. +#[cfg(feature = "std")] pub type MutexGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `RwLockReadGuard` as the owner. +#[cfg(feature = "std")] pub type RwLockReadGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `RwLockWriteGuard` as the owner. +#[cfg(feature = "std")] pub type RwLockWriteGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a mutable owning reference that uses a `Box` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type BoxRefMut = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `Vec` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type VecRefMut = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `String` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type StringRefMut = OwningRefMut; /// Typedef of a mutable owning reference that uses a `RefMut` as the owner. pub type RefMutRefMut<'a, T, U = T> = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `MutexGuard` as the owner. +#[cfg(feature = "std")] pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `RwLockWriteGuard` as the owner. +#[cfg(feature = "std")] pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRefMut, U>; +#[cfg(any(feature = "alloc", feature = "std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Box { type Erased = Box; fn into_erased(self) -> Self::Erased { self } } + +#[cfg(any(feature = "alloc", feature = "std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Rc { type Erased = Rc; fn into_erased(self) -> Self::Erased { self } } + +#[cfg(any(feature = "alloc", feature = "std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Arc { type Erased = Arc; fn into_erased(self) -> Self::Erased { @@ -1154,16 +1185,37 @@ unsafe impl<'a, T: 'a> IntoErased<'a> for Arc { } /// Typedef of a owning reference that uses an erased `Box` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type ErasedBoxRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Rc` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type ErasedRcRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Arc` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type ErasedArcRef = OwningRef, U>; /// Typedef of a mutable owning reference that uses an erased `Box` as the owner. +#[cfg(any(feature = "alloc", feature = "std"))] pub type ErasedBoxRefMut = OwningRefMut, U>; -#[cfg(test)] +///////////////////////////////////////////////////////////////////////////// +// std facade for no_std +///////////////////////////////////////////////////////////////////////////// + +#[cfg(not(feature = "std"))] +mod std { + #[cfg(feature = "alloc")] + extern crate alloc; + #[cfg(feature = "alloc")] + pub use self::alloc::{boxed, rc, string, sync, vec}; + pub use core::{borrow, cell, cmp, convert, fmt, hash, marker, ops}; +} + +///////////////////////////////////////////////////////////////////////////// +// Tests +///////////////////////////////////////////////////////////////////////////// + +#[cfg(all(test, feature = "std"))] mod tests { mod owning_ref { use super::super::OwningRef;