From 71ea3a7f02cc59c5e891c25edbe270e2d9d9d71d Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Wed, 23 Jan 2019 17:27:06 +0100 Subject: openpgp-ffi: Derive conversion functions. - This is a framework for dealing with ownership and references at the FFI boundary. Previously, we used macros to do that. This change introduces a more idiomatic interface, we use traits converting from raw pointers of a wrapper type to objects, references, or mutable references to a wrapped type. - For now, we use the wrapped type as wrapper type. We merely introduce a new mechanism replacing the macro-based one. - This patch also converts all the derived functions. - The following patches will convert all the functions that are already using the ffi_wrapper_type. Once this conversion is done, we can introduce our own wrapper type. --- openpgp-ffi/src/common.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'openpgp-ffi') diff --git a/openpgp-ffi/src/common.rs b/openpgp-ffi/src/common.rs index f764c4b0..f9a2d465 100644 --- a/openpgp-ffi/src/common.rs +++ b/openpgp-ffi/src/common.rs @@ -223,6 +223,45 @@ macro_rules! maybe_box_raw { } } + +/* Support for sequoia_ffi_macros::ffi_wrapper_type-based object + * handling. */ + +/// Moves an object from C to Rust, taking ownership. +pub(crate) trait MoveFromRaw { + /// Moves this object from C to Rust, taking ownership. + fn move_from_raw(self) -> T; +} + +/// Moves a reference to an object from C to Rust. +pub(crate) trait RefRaw { + /// Moves this reference to an object from C to Rust. + fn ref_raw(self) -> &'static T; +} + +/// Moves a mutable reference to an object from C to Rust. +pub(crate) trait RefMutRaw { + /// Moves this mutable reference to an object from C to Rust. + fn ref_mut_raw(self) -> &'static mut T; +} + +/// Moves an object from Rust to C, releasing ownership. +pub(crate) trait MoveIntoRaw { + /// Moves this object from Rust to C, releasing ownership. + fn move_into_raw(self) -> T; +} + +/// Moves an object from Rust to C, releasing ownership. +pub(crate) trait MoveResultIntoRaw { + /// Moves this object from Rust to C, releasing ownership. + fn move_into_raw(self, errp: Option<&mut *mut ::failure::Error>) -> T; +} + +/// Indicates that a pointer may be NULL. +pub type Maybe = Option<::std::ptr::NonNull>; + +/* Hashing support. */ + /// Builds hashers for computing hashes. /// /// This is used to derive Hasher instances for computing hashes of -- cgit v1.2.3