Struct flutter_rust_bridge::ffi::RustOpaque
source · #[repr(transparent)]pub struct RustOpaque<T: ?Sized + DartSafe> { /* private fields */ }
Expand description
A wrapper to transfer ownership of T to Dart.
This type is equivalent to an Option<Arc<T>>
. The inner pointer may
be None if a nullptr is received from Dart, signifying that this pointer
has been disposed.
Extensions for [sync::RwLock
] and [sync::Mutex
] are provided.
Naming the inner type
When an RustOpaque<T>
is transformed into a Dart type, T’s string
representation undergoes some transformations to become a valid Dart type:
- Rust keywords (dyn, ’static, DartSafe, etc.) are automatically removed.
- ASCII alphanumerics are kept, all other characters are ignored.
Trait objects
Trait objects may be put behind opaque pointers, but they must implement
DartSafe
to be safely sent to Dart. For example, this declaration can
be used across the FFI border:
use flutter_rust_bridge::*;
use std::fmt::Debug;
use std::panic::{UnwindSafe, RefUnwindSafe};
// Rust does not allow multiple non-auto traits in trait objects, so this
// is one workaround.
pub trait DartDebug: DartSafe + Debug {}
impl<T: DartSafe + Debug> DartDebug for T {}
pub struct DebugWrapper(pub RustOpaque<Box<dyn DartDebug>>);
// creating a DebugWrapper using the opaque_dyn macro
let wrap = DebugWrapper(opaque_dyn!("foobar"));
// it's possible to name it directly
pub struct DebugWrapper2(pub RustOpaque<Box<dyn Debug + Send + Sync + UnwindSafe + RefUnwindSafe>>);
Implementations§
source§impl<T: DartSafe> RustOpaque<T>
impl<T: DartSafe> RustOpaque<T>
pub fn try_unwrap(self) -> Result<T, Self>
Trait Implementations§
source§impl<T: DartSafe> From<RustOpaque<T>> for DartAbi
impl<T: DartSafe> From<RustOpaque<T>> for DartAbi
source§fn from(value: RustOpaque<T>) -> Self
fn from(value: RustOpaque<T>) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl<T: ?Sized> RefUnwindSafe for RustOpaque<T>
impl<T: ?Sized> Send for RustOpaque<T>where T: Send + Sync,
impl<T: ?Sized> Sync for RustOpaque<T>where T: Send + Sync,
impl<T: ?Sized> Unpin for RustOpaque<T>
impl<T: ?Sized> UnwindSafe for RustOpaque<T>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> IntoDart for Twhere
T: Into<DartCObject>,
impl<T> IntoDart for Twhere T: Into<DartCObject>,
source§fn into_dart(self) -> DartCObject
fn into_dart(self) -> DartCObject
Consumes
Self
and Performs the conversion.