#[repr(C)]
union DiplomatResultValue<T, E> {
ok: ManuallyDrop<T>,
err: ManuallyDrop<E>,
}
/// A [`Result`]-like type that can be passed across the FFI boundary /// as a value. Used internally to return [`Result`]s and [`Option`]s /// from functions. #[repr(C)] pubstruct DiplomatResult<T, E> {
value: DiplomatResultValue<T, E>, pub is_ok: bool,
}
/// A type to represent Option<T> over FFI. /// /// Used internally to handle `Option<T>` arguments and return types, and needs to be /// used explicitly for optional struct fields. pubtype DiplomatOption<T> = DiplomatResult<T, ()>;
impl<T, E> DiplomatResult<T, E> { pubfn as_ref(&self) -> Result<&T, &E> { // Safety: we're only accessing the union variants when the flag is correct unsafe { ifself.is_ok {
Ok(&self.value.ok)
} else {
Err(&self.value.err)
}
}
}
}
impl<T> DiplomatOption<T> { /// Helper for converting into an Option to avoid trait ambiguity errors with Into #[inline] pubfn into_option(self) -> Option<T> { self.into()
}
/// Helper for converting into an Option with the inner type converted #[inline] pubfn into_converted_option<U: From<T>>(self) -> Option<U> { self.into_option().map(Into::into)
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.