Struct runestick::Shared[][src]

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

A shared value.

Implementations

impl<T> Shared<T>[src]

pub fn new(data: T) -> Self[src]

Construct a new shared value.

pub fn debug(&self) -> SharedDebug<'_, T>[src]

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

pub fn is_readable(&self) -> bool[src]

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());

pub fn is_writable(&self) -> bool[src]

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());

pub fn take(self) -> Result<T, AccessError>[src]

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);

pub fn into_ref(self) -> Result<Ref<T>, AccessError>[src]

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);

pub fn into_mut(self) -> Result<Mut<T>, AccessError>[src]

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);

impl<T: ?Sized> Shared<T>[src]

pub fn borrow_ref(&self) -> Result<BorrowRef<'_, T>, AccessError>[src]

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);

pub fn borrow_mut(&self) -> Result<BorrowMut<'_, T>, AccessError>[src]

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);

impl Shared<AnyObj>[src]

pub unsafe fn from_ref<T>(data: &T) -> (Self, SharedPointerGuard) where
    T: Any
[src]

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());
}

pub unsafe fn from_mut<T>(data: &mut T) -> (Self, SharedPointerGuard) where
    T: Any
[src]

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());
}

pub fn take_downcast<T>(self) -> Result<T, AccessError> where
    T: Any
[src]

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

pub fn downcast_borrow_ref<T>(&self) -> Result<BorrowRef<'_, T>, AccessError> where
    T: Any
[src]

Get an shared, downcasted reference to the contained value.

pub fn downcast_borrow_mut<T>(&self) -> Result<BorrowMut<'_, T>, AccessError> where
    T: Any
[src]

Get an exclusive, downcasted reference to the contained value.

pub fn downcast_into_ref<T>(self) -> Result<Ref<T>, AccessError> where
    T: Any
[src]

Get a shared value and downcast.

pub fn downcast_into_mut<T>(self) -> Result<Mut<T>, AccessError> where
    T: Any
[src]

Get an exclusive value and downcast.

Trait Implementations

impl<T: ?Sized> Clone for Shared<T>[src]

impl<T: ?Sized> Debug for Shared<T> where
    T: Debug
[src]

impl<T: ?Sized> Drop for Shared<T>[src]

impl From<Shared<AnyObj>> for Value[src]

impl From<Shared<Bytes>> for Value[src]

impl From<Shared<FunctionImpl<Value>>> for Value[src]

impl From<Shared<Future>> for Value[src]

impl From<Shared<Generator>> for Value[src]

impl From<Shared<GeneratorState>> for Value[src]

impl From<Shared<Iterator>> for Value[src]

impl From<Shared<Object>> for Value[src]

impl From<Shared<Option<Value>>> for Value[src]

impl From<Shared<Range>> for Value[src]

impl From<Shared<Result<Value, Value>>> for Value[src]

impl From<Shared<Stream>> for Value[src]

impl From<Shared<String>> for Value[src]

impl From<Shared<Struct>> for Value[src]

impl From<Shared<Tuple>> for Value[src]

impl From<Shared<TupleStruct>> for Value[src]

impl From<Shared<UnitStruct>> for Value[src]

impl From<Shared<Variant>> for Value[src]

impl From<Shared<Vec>> for Value[src]

impl FromValue for Shared<AnyObj>[src]

impl FromValue for Shared<Function>[src]

impl FromValue for Shared<Future>[src]

impl FromValue for Shared<Generator>[src]

impl FromValue for Shared<GeneratorState>[src]

impl FromValue for Shared<Stream>[src]

impl ToValue for Shared<Option<Value>>[src]

impl ToValue for Shared<Result<Value, Value>>[src]

impl ToValue for Shared<Stream>[src]

impl ToValue for Shared<Generator>[src]

impl ToValue for Shared<GeneratorState>[src]

impl ToValue for Shared<UnitStruct>[src]

impl ToValue for Shared<TupleStruct>[src]

impl ToValue for Shared<Struct>[src]

impl ToValue for Shared<Variant>[src]

impl ToValue for Shared<Function>[src]

impl ToValue for Shared<AnyObj>[src]

impl ToValue for Shared<Iterator>[src]

impl ToValue for Shared<Bytes>[src]

impl ToValue for Shared<String>[src]

impl ToValue for Shared<Vec>[src]

impl ToValue for Shared<Tuple>[src]

impl ToValue for Shared<Object>[src]

impl ToValue for Shared<Range>[src]

impl ToValue for Shared<Future>[src]

impl TypeOf for Shared<Function>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Shared<T>

impl<T> !Send for Shared<T>

impl<T> !Sync for Shared<T>

impl<T: ?Sized> Unpin for Shared<T>

impl<T> !UnwindSafe for Shared<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.