Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ keywords = ["reference", "sibling", "field", "owning"]

[dependencies]
stable_deref_trait = "1.0.0"

[features]
no_std= []
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(missing_docs)]
#![cfg_attr(feature = "no_std", no_std)]

/*!
# An owning reference.
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -480,6 +484,7 @@ impl<O, T: ?Sized> OwningRef<O, T> {
///
/// This can be used to safely erase the owner of any `OwningRef<O, T>`
/// to a `OwningRef<Box<dyn Erased>, T>`.
#[cfg(not(feature = "no_std"))]
pub fn map_owner_box(self) -> OwningRef<Box<O>, T> {
OwningRef {
reference: self.reference,
Expand Down Expand Up @@ -724,6 +729,7 @@ impl<O, T: ?Sized> OwningRefMut<O, T> {
///
/// This can be used to safely erase the owner of any `OwningRefMut<O, T>`
/// to a `OwningRefMut<Box<dyn Erased>, T>`.
#[cfg(not(feature = "no_std"))]
pub fn map_owner_box(self) -> OwningRefMut<Box<O>, T> {
OwningRefMut {
reference: self.reference,
Expand Down Expand Up @@ -1142,9 +1148,13 @@ impl<O, T: ?Sized> Hash for OwningRefMut<O, T> 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};

Expand All @@ -1163,54 +1173,70 @@ impl<T: 'static> ToHandleMut for RefCell<T> {
// 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<T, U = T> = OwningRef<Box<T>, U>;
/// Typedef of a owning reference that uses a `Vec` as the owner.
#[cfg(not(feature = "no_std"))]
pub type VecRef<T, U = T> = OwningRef<Vec<T>, U>;
/// Typedef of a owning reference that uses a `String` as the owner.
#[cfg(not(feature = "no_std"))]
pub type StringRef = OwningRef<String, str>;

/// Typedef of a owning reference that uses a `Rc` as the owner.
#[cfg(not(feature = "no_std"))]
pub type RcRef<T, U = T> = OwningRef<Rc<T>, U>;
/// Typedef of a owning reference that uses a `Arc` as the owner.
#[cfg(not(feature = "no_std"))]
pub type ArcRef<T, U = T> = OwningRef<Arc<T>, U>;

/// Typedef of a owning reference that uses a `Ref` as the owner.
pub type RefRef<'a, T, U = T> = OwningRef<Ref<'a, T>, U>;
/// Typedef of a owning reference that uses a `RefMut` as the owner.
pub type RefMutRef<'a, T, U = T> = OwningRef<RefMut<'a, T>, 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<MutexGuard<'a, T>, 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<RwLockReadGuard<'a, T>, 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<RwLockWriteGuard<'a, T>, U>;

/// Typedef of a mutable owning reference that uses a `Box` as the owner.
#[cfg(not(feature = "no_std"))]
pub type BoxRefMut<T, U = T> = OwningRefMut<Box<T>, U>;
/// Typedef of a mutable owning reference that uses a `Vec` as the owner.
#[cfg(not(feature = "no_std"))]
pub type VecRefMut<T, U = T> = OwningRefMut<Vec<T>, U>;
/// Typedef of a mutable owning reference that uses a `String` as the owner.
#[cfg(not(feature = "no_std"))]
pub type StringRefMut = OwningRefMut<String, str>;

/// Typedef of a mutable owning reference that uses a `RefMut` as the owner.
pub type RefMutRefMut<'a, T, U = T> = OwningRefMut<RefMut<'a, T>, 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<MutexGuard<'a, T>, 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<RwLockWriteGuard<'a, T>, U>;

#[cfg(not(feature = "no_std"))]
unsafe impl<'a, T: 'a> IntoErased<'a> for Box<T> {
type Erased = Box<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
self
}
}
#[cfg(not(feature = "no_std"))]
unsafe impl<'a, T: 'a> IntoErased<'a> for Rc<T> {
type Erased = Rc<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
self
}
}
#[cfg(not(feature = "no_std"))]
unsafe impl<'a, T: 'a> IntoErased<'a> for Arc<T> {
type Erased = Arc<dyn Erased + 'a>;
fn into_erased(self) -> Self::Erased {
Expand All @@ -1219,16 +1245,21 @@ unsafe impl<'a, T: 'a> IntoErased<'a> for Arc<T> {
}

/// Typedef of a owning reference that uses an erased `Box` as the owner.
#[cfg(not(feature = "no_std"))]
pub type ErasedBoxRef<U> = OwningRef<Box<dyn Erased>, U>;
/// Typedef of a owning reference that uses an erased `Rc` as the owner.
#[cfg(not(feature = "no_std"))]
pub type ErasedRcRef<U> = OwningRef<Rc<dyn Erased>, U>;
/// Typedef of a owning reference that uses an erased `Arc` as the owner.
#[cfg(not(feature = "no_std"))]
pub type ErasedArcRef<U> = OwningRef<Arc<dyn Erased>, U>;

/// Typedef of a mutable owning reference that uses an erased `Box` as the owner.
#[cfg(not(feature = "no_std"))]
pub type ErasedBoxRefMut<U> = OwningRefMut<Box<dyn Erased>, U>;

#[cfg(test)]
#[cfg(not(feature = "no_std"))]
mod tests {
mod owning_ref {
use super::super::OwningRef;
Expand Down