pub struct Ref<'a, T>where
T: ?Sized,{ /* private fields */ }
Expand description
Wrapper for a borrowed AtomicCell
that will released lock on drop.
This type can be dereferenced to &T
.
Implements Borrow<T>
and AsRef<T>
for convenience.
Implements Debug
, Display
, PartialEq
, PartialOrd
and Hash
by delegating to T
.
Implementations§
§impl<'a, T> Ref<'a, T>where
T: ?Sized,
impl<'a, T> Ref<'a, T>where
T: ?Sized,
pub fn new(r: &'a T) -> Ref<'a, T>
pub fn new(r: &'a T) -> Ref<'a, T>
Wraps external reference into Ref
.
This function’s purpose is to satisfy type requirements
where Ref
is required but reference does not live in AtomicCell
.
§Examples
use atomicell::Ref;
let r = Ref::new(&42);
pub fn with_borrow(r: &'a T, borrow: AtomicBorrow<'a>) -> Ref<'a, T>
pub fn with_borrow(r: &'a T, borrow: AtomicBorrow<'a>) -> Ref<'a, T>
Wraps external reference into Ref
.
And associates it with provided AtomicBorrow
This function is intended to be used by AtomicCell
or other abstractions that use AtomicBorrow
for locking.
§Examples
use core::sync::atomic::AtomicIsize;
use atomicell::{borrow::{AtomicBorrow, new_lock}, Ref};
let counter = new_lock();
let borrow = AtomicBorrow::try_new(&counter).unwrap();
let r = Ref::with_borrow(&42, borrow);
assert_eq!(*r, 42);
pub fn into_split(r: Ref<'a, T>) -> (NonNull<T>, AtomicBorrow<'a>)
pub fn into_split(r: Ref<'a, T>) -> (NonNull<T>, AtomicBorrow<'a>)
Splits wrapper into two parts. One is reference to the value and the other is
AtomicBorrow
that guards it from being borrowed mutably.
§Safety
User must ensure NonNull
is not dereferenced after AtomicBorrow
is dropped.
Also, the NonNull<T>
that is returned is still only valid for reads, not writes.
§Examples
use atomicell::{AtomicCell, Ref};
let cell = AtomicCell::new(42);
let r: Ref<'_, i32> = cell.borrow();
unsafe {
let (r, borrow) = Ref::into_split(r);
assert_eq!(*r.as_ref(), 42);
assert!(cell.try_borrow().is_some(), "Must be able to borrow immutably");
assert!(cell.try_borrow_mut().is_none(), "Must not be able to borrow mutably yet");
drop(borrow);
assert!(cell.try_borrow_mut().is_some(), "Must be able to borrow mutably now");
}
pub fn map<F, U>(r: Ref<'a, T>, f: F) -> Ref<'a, U>
pub fn map<F, U>(r: Ref<'a, T>, f: F) -> Ref<'a, U>
Makes a new Ref
for a component of the borrowed data.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map(…).
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c = AtomicCell::new((5, 'b'));
let b1: Ref<(u32, char)> = c.borrow();
let b2: Ref<u32> = Ref::map(b1, |t| &t.0);
assert_eq!(*b2, 5)
pub fn filter_map<U, F>(r: Ref<'a, T>, f: F) -> Result<Ref<'a, U>, Ref<'a, T>>
pub fn filter_map<U, F>(r: Ref<'a, T>, f: F) -> Result<Ref<'a, U>, Ref<'a, T>>
Makes a new Ref
for an optional component of the borrowed data.
The original guard is returned as an Err(..) if the closure returns None.
The AtomicCell
is already mutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::filter_map(…).
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c = AtomicCell::new(vec![1, 2, 3]);
let b1: Ref<Vec<u32>> = c.borrow();
let b2: Result<Ref<u32>, _> = Ref::filter_map(b1, |v| v.get(1));
assert_eq!(*b2.unwrap(), 2);
pub fn map_split<U, V, F>(r: Ref<'a, T>, f: F) -> (Ref<'a, U>, Ref<'a, V>)
pub fn map_split<U, V, F>(r: Ref<'a, T>, f: F) -> (Ref<'a, U>, Ref<'a, V>)
Splits a Ref
into multiple Ref
s for different components of the borrowed data.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map_split(...)
.
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{Ref, AtomicCell};
let cell = AtomicCell::new([1, 2, 3, 4]);
let borrow = cell.borrow();
let (begin, end) = Ref::map_split(borrow, |slice| slice.split_at(2));
assert_eq!(*begin, [1, 2]);
assert_eq!(*end, [3, 4]);
pub fn leak(r: Ref<'a, T>) -> &'a T
pub fn leak(r: Ref<'a, T>) -> &'a T
Convert into a reference to the underlying data.
The underlying AtomicCell
can never be mutably borrowed from again
and will always appear already immutably borrowed.
It is not a good idea to leak more than a constant number of references.
The AtomicCell
can be immutably borrowed again if only a smaller number of leaks have occurred in total.
This is an associated function that needs to be used as Ref::leak(…).
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let cell = AtomicCell::new(0);
let value = Ref::leak(cell.borrow());
assert_eq!(*value, 0);
assert!(cell.try_borrow().is_some());
assert!(cell.try_borrow_mut().is_none());
pub fn as_ref<U>(r: Ref<'a, T>) -> Ref<'a, U>
pub fn as_ref<U>(r: Ref<'a, T>) -> Ref<'a, U>
Converts reference and returns result wrapped in the Ref
.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map_split(...)
.
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c = AtomicCell::new(String::from("hello"));
let b1: Ref<String> = c.borrow();
let b2: Ref<str> = Ref::as_ref(b1);
assert_eq!(*b2, *"hello")
pub fn as_deref(r: Ref<'a, T>) -> Ref<'a, <T as Deref>::Target>where
T: Deref,
pub fn as_deref(r: Ref<'a, T>) -> Ref<'a, <T as Deref>::Target>where
T: Deref,
Dereferences and returns result wrapped in the Ref
.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map_split(...)
.
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c = AtomicCell::new(String::from("hello"));
let b1: Ref<String> = c.borrow();
let b2: Ref<str> = Ref::as_deref(b1);
assert_eq!(*b2, *"hello")
§impl<'a, T> Ref<'a, Option<T>>
impl<'a, T> Ref<'a, Option<T>>
pub fn transpose(r: Ref<'a, Option<T>>) -> Option<Ref<'a, T>>
pub fn transpose(r: Ref<'a, Option<T>>) -> Option<Ref<'a, T>>
Transposes a Ref
of an Option
into an Option
of a Ref
.
Releases shared lock of AtomicCell
if the value is None
.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map_split(...)
.
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c = AtomicCell::new(Some(5));
let b1: Ref<Option<i32>> = c.borrow();
let b2: Option<Ref<i32>> = Ref::transpose(b1);
assert!(b2.is_some());
let c = AtomicCell::new(None);
let b1: Ref<Option<i32>> = c.borrow();
let b2: Option<Ref<i32>> = Ref::transpose(b1);
assert!(b2.is_none());
assert!(c.try_borrow_mut().is_some());
§impl<'a, T> Ref<'a, [T]>
impl<'a, T> Ref<'a, [T]>
pub fn slice<R>(r: Ref<'a, [T]>, range: R) -> Ref<'a, [T]>where
R: RangeBounds<usize>,
pub fn slice<R>(r: Ref<'a, [T]>, range: R) -> Ref<'a, [T]>where
R: RangeBounds<usize>,
Makes a new Ref
for a sub-slice of the borrowed slice.
The AtomicCell
is already immutably borrowed, so this cannot fail.
This is an associated function that needs to be used as Ref::map(…).
A method would interfere with methods of the same name on the contents of a AtomicCell
used through Deref
.
§Examples
use atomicell::{AtomicCell, Ref};
let c: &AtomicCell<[i32]> = &AtomicCell::new([1, 2, 3, 4, 5]);
let b1: Ref<[i32]> = c.borrow();
let b2: Ref<[i32]> = Ref::slice(b1, 2..4);
assert_eq!(*b2, [3, 4])
Trait Implementations§
§impl<'a, T, U> PartialOrd<U> for Ref<'a, T>where
T: PartialOrd<U> + ?Sized,
impl<'a, T, U> PartialOrd<U> for Ref<'a, T>where
T: PartialOrd<U> + ?Sized,
§impl<'a> QueryItem for &'a Ref<'a, UntypedComponentStore>
impl<'a> QueryItem for &'a Ref<'a, UntypedComponentStore>
§type Iter = UntypedComponentBitsetIterator<'a>
type Iter = UntypedComponentBitsetIterator<'a>
§fn apply_bitset(&self, bitset: &mut BitSetVec)
fn apply_bitset(&self, bitset: &mut BitSetVec)
§fn get_single_with_bitset(
self,
bitset: Rc<BitSetVec>,
) -> Result<<<&'a Ref<'a, UntypedComponentStore> as QueryItem>::Iter as Iterator>::Item, QuerySingleError>
fn get_single_with_bitset( self, bitset: Rc<BitSetVec>, ) -> Result<<<&'a Ref<'a, UntypedComponentStore> as QueryItem>::Iter as Iterator>::Item, QuerySingleError>
§fn iter_with_bitset(
self,
bitset: Rc<BitSetVec>,
) -> <&'a Ref<'a, UntypedComponentStore> as QueryItem>::Iter
fn iter_with_bitset( self, bitset: Rc<BitSetVec>, ) -> <&'a Ref<'a, UntypedComponentStore> as QueryItem>::Iter
§impl<'a, 'q, T> QueryItem for &'a Ref<'q, ComponentStore<T>>where
T: HasSchema,
impl<'a, 'q, T> QueryItem for &'a Ref<'q, ComponentStore<T>>where
T: HasSchema,
§type Iter = Map<UntypedComponentBitsetIterator<'a>, for<'b> fn(_: SchemaRef<'b>) -> &'b T>
type Iter = Map<UntypedComponentBitsetIterator<'a>, for<'b> fn(_: SchemaRef<'b>) -> &'b T>
§fn apply_bitset(&self, bitset: &mut BitSetVec)
fn apply_bitset(&self, bitset: &mut BitSetVec)
§fn get_single_with_bitset(
self,
bitset: Rc<BitSetVec>,
) -> Result<<<&'a Ref<'q, ComponentStore<T>> as QueryItem>::Iter as Iterator>::Item, QuerySingleError>
fn get_single_with_bitset( self, bitset: Rc<BitSetVec>, ) -> Result<<<&'a Ref<'q, ComponentStore<T>> as QueryItem>::Iter as Iterator>::Item, QuerySingleError>
§fn iter_with_bitset(
self,
bitset: Rc<BitSetVec>,
) -> <&'a Ref<'q, ComponentStore<T>> as QueryItem>::Iter
fn iter_with_bitset( self, bitset: Rc<BitSetVec>, ) -> <&'a Ref<'q, ComponentStore<T>> as QueryItem>::Iter
§impl<'a, T> SystemParam for Ref<'a, ComponentStore<T>>where
T: HasSchema,
impl<'a, T> SystemParam for Ref<'a, ComponentStore<T>>where
T: HasSchema,
§type State = Arc<AtomicCell<ComponentStore<T>>>
type State = Arc<AtomicCell<ComponentStore<T>>>
§type Param<'p> = Ref<'p, ComponentStore<T>>
type Param<'p> = Ref<'p, ComponentStore<T>>
§fn get_state(
world: &World,
) -> <Ref<'a, ComponentStore<T>> as SystemParam>::State
fn get_state( world: &World, ) -> <Ref<'a, ComponentStore<T>> as SystemParam>::State
§fn borrow<'s>(
_world: &'s World,
state: &'s mut <Ref<'a, ComponentStore<T>> as SystemParam>::State,
) -> <Ref<'a, ComponentStore<T>> as SystemParam>::Param<'s>
fn borrow<'s>( _world: &'s World, state: &'s mut <Ref<'a, ComponentStore<T>> as SystemParam>::State, ) -> <Ref<'a, ComponentStore<T>> as SystemParam>::Param<'s>
impl<'b, T> Send for Ref<'b, T>
impl<'b, T> Sync for Ref<'b, T>
Auto Trait Implementations§
impl<'a, T> Freeze for Ref<'a, T>where
T: ?Sized,
impl<'a, T> RefUnwindSafe for Ref<'a, T>where
T: RefUnwindSafe + ?Sized,
impl<'a, T> Unpin for Ref<'a, T>where
T: ?Sized,
impl<'a, T> UnwindSafe for Ref<'a, T>where
T: RefUnwindSafe + ?Sized,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)