Struct bacon_rajan_cc::Cc
source · pub struct Cc<T: 'static + Trace> { /* private fields */ }
Expand description
A reference-counted pointer type over an immutable value.
See the module level documentation for more details.
Implementations§
source§impl<T: 'static + Trace> Cc<T>
impl<T: 'static + Trace> Cc<T>
sourcepub fn is_unique(&self) -> bool
pub fn is_unique(&self) -> bool
Returns true if there are no other Cc
or Weak<T>
values that share
the same inner value.
Examples
use bacon_rajan_cc;
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
assert_eq!(five.is_unique(), true);
let another_five = five.clone();
assert_eq!(five.is_unique(), false);
assert_eq!(another_five.is_unique(), false);
sourcepub fn try_unwrap(self) -> Result<T, Cc<T>>
pub fn try_unwrap(self) -> Result<T, Cc<T>>
Unwraps the contained value if the Cc<T>
is unique.
If the Cc<T>
is not unique, an Err
is returned with the same Cc<T>
.
Examples
use bacon_rajan_cc::Cc;
let x = Cc::new(3);
assert_eq!(x.try_unwrap(), Ok(3));
let x = Cc::new(4);
let _y = x.clone();
assert_eq!(x.try_unwrap(), Err(Cc::new(4)));
sourcepub fn get_mut(&mut self) -> Option<&mut T>
pub fn get_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the contained value if the Cc<T>
is
unique.
Returns None
if the Cc<T>
is not unique.
Examples
use bacon_rajan_cc::Cc;
let mut x = Cc::new(3);
*Cc::get_mut(&mut x).unwrap() = 4;
assert_eq!(*x, 4);
let _y = x.clone();
assert!(Cc::get_mut(&mut x).is_none());
sourcepub fn strong_count(&self) -> usize
pub fn strong_count(&self) -> usize
Get the number of strong references to this value.
sourcepub fn weak_count(&self) -> usize
pub fn weak_count(&self) -> usize
Get the number of weak references to this value.
source§impl<T: 'static + Clone + Trace> Cc<T>
impl<T: 'static + Clone + Trace> Cc<T>
sourcepub fn make_unique(&mut self) -> &mut T
pub fn make_unique(&mut self) -> &mut T
Make a mutable reference from the given Cc<T>
.
This is also referred to as a copy-on-write operation because the inner data is cloned if the reference count is greater than one.
Examples
use bacon_rajan_cc::Cc;
let mut five = Cc::new(5);
let mut_five = five.make_unique();
Trait Implementations§
source§impl<T: Trace> Clone for Cc<T>
impl<T: Trace> Clone for Cc<T>
source§fn clone(&self) -> Cc<T>
fn clone(&self) -> Cc<T>
Makes a clone of the Cc<T>
.
When you clone an Cc<T>
, it will create another pointer to the data and
increase the strong reference counter.
Examples
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
five.clone();
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Trace> Drop for Cc<T>
impl<T: Trace> Drop for Cc<T>
source§fn drop(&mut self)
fn drop(&mut self)
Drops the Cc<T>
.
This will decrement the strong reference count. If the strong reference
count becomes zero and the only other references are Weak<T>
ones,
drop
s the inner value.
Examples
use bacon_rajan_cc::Cc;
{
let five = Cc::new(5);
// stuff
drop(five); // explicit drop
}
{
let five = Cc::new(5);
// stuff
} // implicit drop
source§impl<T: Ord + Trace> Ord for Cc<T>
impl<T: Ord + Trace> Ord for Cc<T>
source§fn cmp(&self, other: &Cc<T>) -> Ordering
fn cmp(&self, other: &Cc<T>) -> Ordering
Comparison for two Cc<T>
s.
The two are compared by calling cmp()
on their inner values.
Examples
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
five.partial_cmp(&Cc::new(5));
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl<T: PartialEq + Trace> PartialEq<Cc<T>> for Cc<T>
impl<T: PartialEq + Trace> PartialEq<Cc<T>> for Cc<T>
source§impl<T: PartialOrd + Trace> PartialOrd<Cc<T>> for Cc<T>
impl<T: PartialOrd + Trace> PartialOrd<Cc<T>> for Cc<T>
source§fn partial_cmp(&self, other: &Cc<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &Cc<T>) -> Option<Ordering>
Partial comparison for two Cc<T>
s.
The two are compared by calling partial_cmp()
on their inner values.
Examples
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
five.partial_cmp(&Cc::new(5));
source§fn lt(&self, other: &Cc<T>) -> bool
fn lt(&self, other: &Cc<T>) -> bool
Less-than comparison for two Cc<T>
s.
The two are compared by calling <
on their inner values.
Examples
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
five < Cc::new(5);
source§fn le(&self, other: &Cc<T>) -> bool
fn le(&self, other: &Cc<T>) -> bool
‘Less-than or equal to’ comparison for two Cc<T>
s.
The two are compared by calling <=
on their inner values.
Examples
use bacon_rajan_cc::Cc;
let five = Cc::new(5);
five <= Cc::new(5);