Struct wasmtime_runtime::VMExternRef [−][src]
An external reference to some opaque data.
VMExternRef
s dereference to their underlying opaque data as dyn Any
.
Unlike the externref
in the Wasm spec, VMExternRef
s are non-nullable,
and always point to a valid value. You may use Option<VMExternRef>
to
represent nullable references, and Option<VMExternRef>
is guaranteed to
have the same size and alignment as a raw pointer, with None
represented
with the null pointer.
VMExternRef
s are reference counted, so cloning is a cheap, shallow
operation. It also means they are inherently shared, so you may not get a
mutable, exclusive reference to their inner contents, only a shared,
immutable reference. You may use interior mutability with RefCell
or
Mutex
to work around this restriction, if necessary.
VMExternRef
s have pointer-equality semantics, not structural-equality
semantics. Given two VMExternRef
s a
and b
, a == b
only if a
and
b
point to the same allocation. a
and b
are considered not equal, even
if a
and b
are two different identical copies of the same data, if they
are in two different allocations. The hashing and ordering implementations
also only operate on the pointer.
Example
use std::cell::RefCell; use wasmtime_runtime::VMExternRef; // Open a file. Wasm doesn't know about files, but we can let Wasm instances // work with files via opaque `externref` handles. let file = std::fs::File::create("some/file/path")?; // Wrap the file up as an `VMExternRef` that can be passed to Wasm. let extern_ref_to_file = VMExternRef::new(RefCell::new(file)); // `VMExternRef`s dereference to `dyn Any`, so you can use `Any` methods to // perform runtime type checks and downcasts. assert!(extern_ref_to_file.is::<RefCell<std::fs::File>>()); assert!(!extern_ref_to_file.is::<String>()); if let Some(file) = extern_ref_to_file.downcast_ref::<RefCell<std::fs::File>>() { use std::io::Write; let mut file = file.borrow_mut(); writeln!(&mut file, "Hello, `VMExternRef`!")?; }
Implementations
impl VMExternRef
[src]
pub fn new<T>(value: T) -> VMExternRef where
T: 'static + Any,
[src]
T: 'static + Any,
Wrap the given value inside an VMExternRef
.
pub fn new_with<T>(make_value: impl FnOnce() -> T) -> VMExternRef where
T: 'static + Any,
[src]
T: 'static + Any,
Construct a new VMExternRef
in place by invoking make_value
.
pub fn as_raw(&self) -> *mut u8
[src]
Turn this VMExternRef
into a raw, untyped pointer.
Unlike into_raw
, this does not consume and forget self
. It is not
safe to use from_raw
on pointers returned from this method; only use
clone_from_raw
!
Nor does this method increment the reference count. You must ensure
that self
(or some other clone of self
) stays alive until
clone_from_raw
is called.
pub unsafe fn clone_from_raw(ptr: *mut u8) -> Self
[src]
Recreate a VMExternRef
from a pointer returned from a previous call to
VMExternRef::as_raw
.
Safety
Wildly unsafe to use with anything other than the result of a previous
as_raw
call!
Additionally, it is your responsibility to ensure that this raw
VMExternRef
’s reference count has not dropped to zero. Failure to do
so will result in use after free!
pub fn strong_count(&self) -> usize
[src]
Get the strong reference count for this VMExternRef
.
impl VMExternRef
[src]
Methods that would normally be trait implementations, but aren’t to avoid
potential footguns around VMExternRef
’s pointer-equality semantics.
Note that none of these methods are on &self
, they all require a
fully-qualified VMExternRef::foo(my_ref)
invocation.
pub fn eq(a: &Self, b: &Self) -> bool
[src]
Check whether two VMExternRef
s point to the same inner allocation.
Note that this uses pointer-equality semantics, not structural-equality
semantics, and so only pointers are compared, and doesn’t use any Eq
or PartialEq
implementation of the pointed-to values.
pub fn hash<H>(externref: &Self, hasher: &mut H) where
H: Hasher,
[src]
H: Hasher,
Hash a given VMExternRef
.
Note that this just hashes the pointer to the inner value, it does not
use the inner value’s Hash
implementation (if any).
pub fn cmp(a: &Self, b: &Self) -> Ordering
[src]
Compare two VMExternRef
s.
Note that this uses pointer-equality semantics, not structural-equality
semantics, and so only pointers are compared, and doesn’t use any Cmp
or PartialCmp
implementation of the pointed-to values.
Methods from Deref<Target = dyn Any>
pub fn is<T>(&self) -> bool where
T: Any,
1.0.0[src]
T: Any,
Returns true
if the boxed type is the same as T
.
Examples
use std::any::Any; fn is_string(s: &dyn Any) { if s.is::<String>() { println!("It's a string!"); } else { println!("Not a string..."); } } is_string(&0); is_string(&"cookie monster".to_string());
pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,
1.0.0[src]
T: Any,
Returns some reference to the boxed value if it is of type T
, or
None
if it isn’t.
Examples
use std::any::Any; fn print_if_string(s: &dyn Any) { if let Some(string) = s.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); } } print_if_string(&0); print_if_string(&"cookie monster".to_string());
pub fn is<T>(&self) -> bool where
T: Any,
1.0.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn is_string(s: &(dyn Any + Send)) { if s.is::<String>() { println!("It's a string!"); } else { println!("Not a string..."); } } is_string(&0); is_string(&"cookie monster".to_string());
pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,
1.0.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn print_if_string(s: &(dyn Any + Send)) { if let Some(string) = s.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); } } print_if_string(&0); print_if_string(&"cookie monster".to_string());
pub fn is<T>(&self) -> bool where
T: Any,
1.28.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn is_string(s: &(dyn Any + Send + Sync)) { if s.is::<String>() { println!("It's a string!"); } else { println!("Not a string..."); } } is_string(&0); is_string(&"cookie monster".to_string());
pub fn downcast_ref<T>(&self) -> Option<&T> where
T: Any,
1.28.0[src]
T: Any,
Forwards to the method defined on the type Any
.
Examples
use std::any::Any; fn print_if_string(s: &(dyn Any + Send + Sync)) { if let Some(string) = s.downcast_ref::<String>() { println!("It's a string({}): '{}'", string.len(), string); } else { println!("Not a string..."); } } print_if_string(&0); print_if_string(&"cookie monster".to_string());
Trait Implementations
impl Clone for VMExternRef
[src]
fn clone(&self) -> VMExternRef
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for VMExternRef
[src]
impl Deref for VMExternRef
[src]
impl Drop for VMExternRef
[src]
impl From<VMExternRef> for TableElement
[src]
fn from(x: VMExternRef) -> TableElement
[src]
Auto Trait Implementations
impl !RefUnwindSafe for VMExternRef
impl !Send for VMExternRef
impl !Sync for VMExternRef
impl Unpin for VMExternRef
impl !UnwindSafe for VMExternRef
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Pointable for T
[src]
pub const ALIGN: usize
[src]
type Init = T
The type for initializers.
pub unsafe fn init(init: <T as Pointable>::Init) -> usize
[src]
pub unsafe fn deref<'a>(ptr: usize) -> &'a T
[src]
pub unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T
[src]
pub unsafe fn drop(ptr: usize)
[src]
impl<T> Same<T> for T
[src]
type Output = T
Should always be Self
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,