Struct rune::runtime::Shared

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

A shared value.

Implementations§

source§

impl<T> Shared<T>

source

pub fn new(data: T) -> Result<Self>

Construct a new shared value.

source

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

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

source

pub fn is_readable(&self) -> bool

Test if the value is sharable.

§Examples
use rune::runtime::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 rune::runtime::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());
source

pub fn is_writable(&self) -> bool

Test if the value is exclusively accessible.

§Examples
use rune::runtime::Shared;

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

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

assert!(shared.is_writable());
source

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

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 rune::runtime::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);
source

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

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 rune::runtime::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);
source

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

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 rune::runtime::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);
source§

impl<T: ?Sized> Shared<T>

source

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

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 rune::runtime::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);
source

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

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 rune::runtime::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);
source§

impl Shared<AnyObj>

source

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

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 rune::Any;
use rune::runtime::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());
}
source

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

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 rune::Any;
use rune::runtime::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());
}
source

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

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

source

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

Get an shared, downcasted reference to the contained value.

source

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

Get an exclusive, downcasted reference to the contained value.

source

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

Get a shared value and downcast.

source

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

Get an exclusive value and downcast.

Trait Implementations§

source§

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

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for Shared<T>
where T: Debug + ?Sized,

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl From<Shared<AnyObj>> for Value

source§

fn from(value: Shared<AnyObj>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Bytes>> for Value

source§

fn from(value: Shared<Bytes>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<ControlFlow>> for Value

source§

fn from(value: Shared<ControlFlow>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<EmptyStruct>> for Value

source§

fn from(value: Shared<EmptyStruct>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Format>> for Value

source§

fn from(value: Shared<Format>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Function>> for Value

source§

fn from(value: Shared<Function>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Future>> for Value

source§

fn from(value: Shared<Future>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Generator<Vm>>> for Value

source§

fn from(value: Shared<Generator<Vm>>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<GeneratorState>> for Value

source§

fn from(value: Shared<GeneratorState>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Iterator>> for Value

source§

fn from(value: Shared<Iterator>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Object>> for Value

source§

fn from(value: Shared<Object>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Option<Value>>> for Value

source§

fn from(value: Shared<Option<Value>>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<OwnedTuple>> for Value

source§

fn from(value: Shared<OwnedTuple>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Range>> for Value

source§

fn from(value: Shared<Range>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<RangeFrom>> for Value

source§

fn from(value: Shared<RangeFrom>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<RangeFull>> for Value

source§

fn from(value: Shared<RangeFull>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<RangeInclusive>> for Value

source§

fn from(value: Shared<RangeInclusive>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<RangeTo>> for Value

source§

fn from(value: Shared<RangeTo>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<RangeToInclusive>> for Value

source§

fn from(value: Shared<RangeToInclusive>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Result<Value, Value>>> for Value

source§

fn from(value: Shared<Result<Value, Value>>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Stream<Vm>>> for Value

source§

fn from(value: Shared<Stream<Vm>>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<String>> for Value

source§

fn from(value: Shared<String>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Struct>> for Value

source§

fn from(value: Shared<Struct>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<TupleStruct>> for Value

source§

fn from(value: Shared<TupleStruct>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Variant>> for Value

source§

fn from(value: Shared<Variant>) -> Self

Converts to this type from the input type.
source§

impl From<Shared<Vec>> for Value

source§

fn from(value: Shared<Vec>) -> Self

Converts to this type from the input type.
source§

impl FromValue for Shared<AnyObj>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Bytes>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<ControlFlow>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Format>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Function>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Future>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Generator<Vm>>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<GeneratorState>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Iterator>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Object>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Range>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<RangeFrom>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<RangeFull>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<RangeInclusive>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<RangeTo>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<RangeToInclusive>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Stream<Vm>>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl FromValue for Shared<Vec>

source§

fn from_value(value: Value) -> VmResult<Self>

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

impl<T> MaybeTypeOf for Shared<T>
where T: ?Sized + MaybeTypeOf,

source§

fn maybe_type_of() -> Option<FullTypeOf>

Type information for the given type.
source§

impl ToValue for Shared<AnyObj>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Bytes>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<ControlFlow>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<EmptyStruct>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Format>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Function>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Future>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Generator<Vm>>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<GeneratorState>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Iterator>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Object>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Option<Value>>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<OwnedTuple>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Range>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<RangeFrom>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<RangeFull>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<RangeInclusive>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<RangeTo>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<RangeToInclusive>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Result<Value, Value>>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Stream<Vm>>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<String>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Struct>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<TupleStruct>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Variant>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

impl ToValue for Shared<Vec>

source§

fn to_value(self) -> VmResult<Value>

Convert into a value.
source§

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

source§

fn try_clone(&self) -> Result<Self>

Try to clone the current value, raising an allocation error if it’s unsuccessful.
source§

fn try_clone_from(&mut self, source: &Self) -> Result<(), Error>

Performs copy-assignment from source. Read more
source§

impl<T> TypeOf for Shared<T>
where T: ?Sized + TypeOf,

Blanket implementation for owned shared values.

source§

fn type_of() -> FullTypeOf

Type information for the given type.
source§

fn type_parameters() -> Hash

Hash of type parameters.
source§

fn type_hash() -> Hash

Get full type hash, including type parameters.
source§

fn type_info() -> TypeInfo

Access diagnostical information on the value type.

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> TryToOwned for T
where T: TryClone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn try_to_owned(&self) -> Result<T, Error>

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

impl<T> UnsafeToValue for T
where T: ToValue,

§

type Guard = ()

The type used to guard the unsafe value conversion.
source§

unsafe fn unsafe_to_value( self ) -> VmResult<(Value, <T as UnsafeToValue>::Guard)>

Convert into a value. Read more
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more