Struct runestick::Shared[][src]

pub struct Shared<T: ?Sized> { /* fields omitted */ }
Expand description

A shared value.

Implementations

Construct a new shared value.

Return a debug formatter, that when printed will display detailed diagnostics of this shared type.

Test if the value is sharable.

Examples

use runestick::Shared;
let shared = Shared::new(1u32);
assert!(shared.is_readable());

{
    let guard = shared.borrow_ref().unwrap();
    assert!(shared.is_readable()); // Note: still readable.
}

{
    let guard = shared.borrow_mut().unwrap();
    assert!(!shared.is_readable());
}

assert!(shared.is_readable());

Taking inner value

use runestick::Shared;
let shared = Shared::new(1u32);
let shared2 = shared.clone();
assert!(shared.is_readable());
shared.take().unwrap();
assert!(!shared2.is_readable());
assert!(shared2.take().is_err());

Test if the value is exclusively accessible.

Examples

use runestick::Shared;
let shared = Shared::new(1u32);
assert!(shared.is_writable());

{
    let guard = shared.borrow_ref().unwrap();
    assert!(!shared.is_writable());
}

assert!(shared.is_writable());

Take the interior value, if we have exlusive access to it and there are no other live exlusive or shared references.

A value that has been taken can no longer be accessed.

Examples

use runestick::Shared;

#[derive(Debug)]
struct Foo {
    counter: isize,
}

let a = Shared::new(Foo { counter: 0 });
let b = a.clone();

{
    let mut a = a.borrow_mut().unwrap();
    // NB: this is prevented since we have a live reference.
    assert!(b.take().is_err());
    a.counter += 1;
}

let a = a.take().unwrap();
assert_eq!(a.counter, 1);

Get a reference to the interior value while checking for shared access that holds onto a reference count of the inner value.

This prevents other exclusive accesses from being performed while the guard returned from this function is live.

Examples

use runestick::Shared;

#[derive(Debug)]
struct Foo {
    counter: isize,
}

let a = Shared::new(Foo { counter: 0 });
let b = a.clone();

b.borrow_mut().unwrap().counter += 1;

{
    // Consumes `a`.
    let mut a = a.into_ref().unwrap();
    assert_eq!(a.counter, 1);
    assert!(b.borrow_mut().is_err());
}

let mut b = b.borrow_mut().unwrap();
b.counter += 1;
assert_eq!(b.counter, 2);

Get a reference to the interior value while checking for exclusive access that holds onto a reference count of the inner value.

This prevents other exclusive and shared accesses from being performed while the guard returned from this function is live.

Examples

use runestick::Shared;

#[derive(Debug)]
struct Foo {
    counter: isize,
}

let a = Shared::new(Foo { counter: 0 });
let b = a.clone();

{
    // Consumes `a`.
    let mut a = a.into_mut().unwrap();
    a.counter += 1;

    assert!(b.borrow_ref().is_err());
}

assert_eq!(b.borrow_ref().unwrap().counter, 1);

Get a reference to the interior value while checking for shared access.

This prevents other exclusive accesses from being performed while the guard returned from this function is live.

Examples

use runestick::Shared;

#[derive(Debug)]
struct Foo {
    counter: isize,
}

let a = Shared::new(Foo { counter: 0 });

a.borrow_mut().unwrap().counter += 1;

{
    let mut a_ref = a.borrow_ref().unwrap();
    assert_eq!(a_ref.counter, 1);
    assert!(a.borrow_mut().is_err());
    assert!(a.borrow_ref().is_ok());
}

let mut a = a.borrow_mut().unwrap();
a.counter += 1;
assert_eq!(a.counter, 2);

Get a reference to the interior value while checking for exclusive access.

This prevents other shared or exclusive accesses from being performed while the guard returned from this function is live.

Examples

use runestick::Shared;

#[derive(Debug)]
struct Foo {
    counter: isize,
}

let a = Shared::new(Foo { counter: 0 });

{
    let mut a_mut = a.borrow_mut().unwrap();
    a_mut.counter += 1;
    assert_eq!(a_mut.counter, 1);
    assert!(a.borrow_ref().is_err());
}

let a = a.borrow_ref().unwrap();
assert_eq!(a.counter, 1);

Construct a Shared<Any> from a pointer, that will be “taken” once the returned guard is dropped.

Safety

The reference must be valid for the duration of the guard.

Examples

use runestick::{Any, Shared};

#[derive(Any)]
struct Thing(u32);

let value = Thing(10u32);

unsafe {
    let (shared, guard) = Shared::from_ref(&value);
    assert!(shared.downcast_borrow_mut::<Thing>().is_err());
    assert_eq!(10u32, shared.downcast_borrow_ref::<Thing>().unwrap().0);

    drop(guard);

    assert!(shared.downcast_borrow_mut::<Thing>().is_err());
    assert!(shared.downcast_borrow_ref::<Thing>().is_err());
}

Construct a Shared<Any> from a mutable pointer, that will be “taken” once the returned guard is dropped.

Safety

The reference must be valid for the duration of the guard.

Examples

use runestick::{Any, Shared};

#[derive(Any)]
struct Thing(u32);

let mut value = Thing(10u32);

unsafe {
    let (shared, guard) = Shared::from_mut(&mut value);
    shared.downcast_borrow_mut::<Thing>().unwrap().0 = 20;

    assert_eq!(20u32, shared.downcast_borrow_mut::<Thing>().unwrap().0);
    assert_eq!(20u32, shared.downcast_borrow_ref::<Thing>().unwrap().0);

    drop(guard);

    assert!(shared.downcast_borrow_mut::<Thing>().is_err());
    assert!(shared.downcast_borrow_ref::<Thing>().is_err());
}

Take the interior value, if we have exlusive access to it and there exist no other references.

Get an shared, downcasted reference to the contained value.

Get an exclusive, downcasted reference to the contained value.

Get a shared value and downcast.

Get an exclusive value and downcast.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Try to convert to the given type, from the given value.

Try to convert to the given type, from the given value.

Try to convert to the given type, from the given value.

Try to convert to the given type, from the given value.

Try to convert to the given type, from the given value.

Try to convert to the given type, from the given value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a value.

Convert into a type hash.

Access diagnostical information on the value type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.