pub struct Shared<T> { /* private fields */ }Expand description
A typed wrapper for a reference.
This is identical in layout to AnyObj, but provides a statically
type-checked container.
Implementations§
Sourcepub fn new(value: T) -> Result<Self>
pub fn new(value: T) -> Result<Self>
Construct a new typed shared value.
§Examples
use rune::Value;
use rune::runtime::Shared;
use rune::alloc::String;
let string = String::try_from("Hello World")?;
let string = Shared::new(string)?;
let string = Value::from(string);
let string = string.into_any_obj()?;
assert_eq!(string.borrow_ref::<String>()?.as_str(), "Hello World");Sourcepub fn take(self) -> Result<T, AnyObjError>
pub fn take(self) -> Result<T, AnyObjError>
Take the owned value of type T.
This consumes any live references of the value and accessing them in the future will result in an error.
§Errors
This errors if the underlying value is not owned.
Sourcepub fn into_ref(self) -> Result<Ref<T>, AnyObjError>
pub fn into_ref(self) -> Result<Ref<T>, AnyObjError>
Downcast into an owned value of type Ref<T>.
§Errors
This errors in case the underlying value is not owned, non-owned
references cannot be coerced into Ref<T>.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
let reference = value.clone().into_ref()?;
assert!(value.borrow_ref().is_ok());
assert_eq!(reference.0, 42);Sourcepub fn into_mut(self) -> Result<Mut<T>, AnyObjError>
pub fn into_mut(self) -> Result<Mut<T>, AnyObjError>
Downcast into an owned value of type Mut<T>.
§Errors
This errors in case the underlying value is not owned, non-owned
references cannot be coerced into Mut<T>.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
let mut mutable = value.clone().into_mut()?;
assert!(value.borrow_ref().is_err());
mutable.0 += 1;
drop(mutable);
assert_eq!(value.borrow_ref()?.0, 43);Sourcepub fn borrow_ref(&self) -> Result<BorrowRef<'_, T>, AnyObjError>
pub fn borrow_ref(&self) -> Result<BorrowRef<'_, T>, AnyObjError>
Borrow a shared reference to the 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::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
let borrowed = value.borrow_ref()?;
assert!(value.borrow_ref().is_ok());
drop(borrowed);
assert!(value.borrow_ref().is_ok());Sourcepub fn borrow_mut(&self) -> Result<BorrowMut<'_, T>, AnyObjError>
pub fn borrow_mut(&self) -> Result<BorrowMut<'_, T>, AnyObjError>
Borrow an exclusive reference to the value.
This prevents other accesses from being performed while the guard returned from this function is live.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
let borrowed = value.borrow_mut()?;
assert!(value.borrow_ref().is_err());
drop(borrowed);
assert!(value.borrow_ref().is_ok());Sourcepub fn is_readable(&self) -> bool
pub fn is_readable(&self) -> bool
Test if the value is sharable.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
{
assert!(value.is_writable());
let borrowed = value.borrow_mut()?;
assert!(!value.is_writable());
drop(borrowed);
assert!(value.is_writable());
}
let foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_ref(&foo)? };
let value = value.into_shared::<Struct>()?;
assert!(value.is_readable());
assert!(!value.is_writable());
}
let mut foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_mut(&mut foo)? };
let value = value.into_shared::<Struct>()?;
assert!(value.is_readable());
assert!(value.is_writable());
}Sourcepub fn is_writable(&self) -> bool
pub fn is_writable(&self) -> bool
Test if a value is writable.
§Examples
use rune::{Any, Value};
#[derive(Any)]
struct Struct(u32);
let value = Value::new(Struct(42))?;
let value = value.into_shared::<Struct>()?;
{
assert!(value.is_writable());
let borrowed = value.borrow_mut()?;
assert!(!value.is_writable());
drop(borrowed);
assert!(value.is_writable());
}
let foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_ref(&foo)? };
let value = value.into_shared::<Struct>()?;
assert!(value.is_readable());
assert!(!value.is_writable());
}
let mut foo = Struct(42);
{
let (value, guard) = unsafe { Value::from_mut(&mut foo)? };
let value = value.into_shared::<Struct>()?;
assert!(value.is_readable());
assert!(value.is_writable());
}Trait Implementations§
§Examples
use rune::Value;
use rune::runtime::Shared;
use rune::alloc::String;
let string = String::try_from("Hello World")?;
let string = Shared::new(string)?;
let string = Value::from(string);
let string = string.into_any_obj()?;
assert_eq!(string.borrow_ref::<String>()?.as_str(), "Hello World");