diff --git a/Cargo.toml b/Cargo.toml index 4817fc6..e4e8670 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ keywords = ["reference", "sibling", "field", "owning"] [dependencies] stable_deref_trait = "1.0.0" + +[features] +no_std= [] diff --git a/src/lib.rs b/src/lib.rs index fa2f15a..031eda5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![warn(missing_docs)] +#![cfg_attr(feature = "no_std", no_std)] /*! # An owning reference. @@ -243,6 +244,9 @@ fn main() { ``` */ +#[cfg(feature = "no_std")] +extern crate core as std; + extern crate stable_deref_trait; pub use stable_deref_trait::{StableDeref as StableAddress, CloneStableDeref as CloneStableAddress}; @@ -480,6 +484,7 @@ impl OwningRef { /// /// This can be used to safely erase the owner of any `OwningRef` /// to a `OwningRef, T>`. + #[cfg(not(feature = "no_std"))] pub fn map_owner_box(self) -> OwningRef, T> { OwningRef { reference: self.reference, @@ -724,6 +729,7 @@ impl OwningRefMut { /// /// This can be used to safely erase the owner of any `OwningRefMut` /// to a `OwningRefMut, T>`. + #[cfg(not(feature = "no_std"))] pub fn map_owner_box(self) -> OwningRefMut, T> { OwningRefMut { reference: self.reference, @@ -1142,9 +1148,13 @@ impl Hash for OwningRefMut where T: Hash { // std types integration and convenience type defs ///////////////////////////////////////////////////////////////////////////// +#[cfg(not(feature = "no_std"))] use std::boxed::Box; +#[cfg(not(feature = "no_std"))] use std::rc::Rc; +#[cfg(not(feature = "no_std"))] use std::sync::Arc; +#[cfg(not(feature = "no_std"))] use std::sync::{MutexGuard, RwLockReadGuard, RwLockWriteGuard}; use std::cell::{Ref, RefCell, RefMut}; @@ -1163,15 +1173,20 @@ impl ToHandleMut for RefCell { // what to do with error results. /// Typedef of a owning reference that uses a `Box` as the owner. +#[cfg(not(feature = "no_std"))] pub type BoxRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Vec` as the owner. +#[cfg(not(feature = "no_std"))] pub type VecRef = OwningRef, U>; /// Typedef of a owning reference that uses a `String` as the owner. +#[cfg(not(feature = "no_std"))] pub type StringRef = OwningRef; /// Typedef of a owning reference that uses a `Rc` as the owner. +#[cfg(not(feature = "no_std"))] pub type RcRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Arc` as the owner. +#[cfg(not(feature = "no_std"))] pub type ArcRef = OwningRef, U>; /// Typedef of a owning reference that uses a `Ref` as the owner. @@ -1179,38 +1194,49 @@ 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(not(feature = "no_std"))] pub type MutexGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `RwLockReadGuard` as the owner. +#[cfg(not(feature = "no_std"))] pub type RwLockReadGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a owning reference that uses a `RwLockWriteGuard` as the owner. +#[cfg(not(feature = "no_std"))] pub type RwLockWriteGuardRef<'a, T, U = T> = OwningRef, U>; /// Typedef of a mutable owning reference that uses a `Box` as the owner. +#[cfg(not(feature = "no_std"))] pub type BoxRefMut = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `Vec` as the owner. +#[cfg(not(feature = "no_std"))] pub type VecRefMut = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `String` as the owner. +#[cfg(not(feature = "no_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(not(feature = "no_std"))] pub type MutexGuardRefMut<'a, T, U = T> = OwningRefMut, U>; /// Typedef of a mutable owning reference that uses a `RwLockWriteGuard` as the owner. +#[cfg(not(feature = "no_std"))] pub type RwLockWriteGuardRefMut<'a, T, U = T> = OwningRefMut, U>; +#[cfg(not(feature = "no_std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Box { type Erased = Box; fn into_erased(self) -> Self::Erased { self } } +#[cfg(not(feature = "no_std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Rc { type Erased = Rc; fn into_erased(self) -> Self::Erased { self } } +#[cfg(not(feature = "no_std"))] unsafe impl<'a, T: 'a> IntoErased<'a> for Arc { type Erased = Arc; fn into_erased(self) -> Self::Erased { @@ -1219,16 +1245,21 @@ unsafe impl<'a, T: 'a> IntoErased<'a> for Arc { } /// Typedef of a owning reference that uses an erased `Box` as the owner. +#[cfg(not(feature = "no_std"))] pub type ErasedBoxRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Rc` as the owner. +#[cfg(not(feature = "no_std"))] pub type ErasedRcRef = OwningRef, U>; /// Typedef of a owning reference that uses an erased `Arc` as the owner. +#[cfg(not(feature = "no_std"))] pub type ErasedArcRef = OwningRef, U>; /// Typedef of a mutable owning reference that uses an erased `Box` as the owner. +#[cfg(not(feature = "no_std"))] pub type ErasedBoxRefMut = OwningRefMut, U>; #[cfg(test)] +#[cfg(not(feature = "no_std"))] mod tests { mod owning_ref { use super::super::OwningRef;