#[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>

source

pub fn try_unwrap(self) -> Result<T, Self>

source§

impl<T: DartSafe> RustOpaque<T>

source

pub fn new(value: T) -> Self

Trait Implementations§

source§

impl<T: ?Sized + DartSafe> Clone for RustOpaque<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 + ?Sized + DartSafe> Debug for RustOpaque<T>

source§

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

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

impl<T: ?Sized + DartSafe> Deref for RustOpaque<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T: ?Sized + DartSafe> From<Arc<T>> for RustOpaque<T>

source§

fn from(ptr: Arc<T>) -> Self

Converts to this type from the input type.
source§

impl<T: DartSafe> From<RustOpaque<T>> for DartAbi

source§

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> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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, U> Into<U> for Twhere 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> IntoDart for Twhere T: Into<DartCObject>,

source§

fn into_dart(self) -> DartCObject

Consumes Self and Performs the conversion.
source§

impl<T> ToOwned for Twhere 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 Twhere 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 Twhere 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> DartSafe for Twhere T: UnwindSafe + RefUnwindSafe,

source§

impl<T> IntoDartExceptPrimitive for Twhere T: IntoDart + Into<DartCObject>,