summaryrefslogtreecommitdiffstats
path: root/openpgp-ffi
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-01-23 17:27:06 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-01-25 14:04:26 +0100
commit71ea3a7f02cc59c5e891c25edbe270e2d9d9d71d (patch)
treef518a31f491e6f6816b4014bbe7bc9768bfdc20d /openpgp-ffi
parentd1e68e97496d0541f3ddd0483f62e185e870c9e4 (diff)
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.
Diffstat (limited to 'openpgp-ffi')
-rw-r--r--openpgp-ffi/src/common.rs39
1 files changed, 39 insertions, 0 deletions
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<T> {
+ /// 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<T> {
+ /// 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<T> {
+ /// 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<T> {
+ /// 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<T> {
+ /// 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<T> = Option<::std::ptr::NonNull<T>>;
+
+/* Hashing support. */
+
/// Builds hashers for computing hashes.
///
/// This is used to derive Hasher instances for computing hashes of