Skip to main content

ffi_utils/
lib.rs

1// Copyright 2019 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// http://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10//! FFI utilities.
11
12#![doc(
13    html_logo_url = "https://raw.githubusercontent.com/maidsafe/QA/master/Images/maidsafe_logo.png",
14    html_favicon_url = "http://maidsafe.net/img/favicon.ico",
15    test(attr(forbid(warnings)))
16)]
17// For explanation of lint checks, run `rustc -W help`.
18#![warn(
19    missing_docs,
20    trivial_casts,
21    trivial_numeric_casts,
22    unused_extern_crates,
23    unused_import_braces,
24    unused_qualifications,
25    unused_results
26)]
27// This crate makes liberal use of unsafe code to work with FFI.
28#![allow(unsafe_code)]
29
30pub mod bindgen_utils;
31pub mod callback;
32#[cfg(feature = "java")]
33pub mod java;
34pub mod result;
35pub mod string;
36pub mod test_utils;
37
38mod b64;
39mod catch_unwind;
40mod macros;
41mod repr_c;
42mod vec;
43
44pub use self::b64::{base64_decode, base64_encode};
45pub use self::catch_unwind::{catch_unwind_cb, catch_unwind_result};
46pub use self::repr_c::ReprC;
47pub use self::result::{FfiResult, NativeResult, FFI_RESULT_OK};
48pub use self::string::StringError;
49pub use self::vec::{vec_clone_from_raw_parts, vec_from_raw_parts, vec_into_raw_parts, SafePtr};
50
51use std::os::raw::c_void;
52
53/// Type that holds opaque user data handed into FFI functions.
54#[derive(Clone, Copy)]
55pub struct OpaqueCtx(pub *mut c_void);
56unsafe impl Send for OpaqueCtx {}
57
58impl Into<*mut c_void> for OpaqueCtx {
59    fn into(self) -> *mut c_void {
60        self.0
61    }
62}
63
64/// Trait for types that can be converted to integer error code.
65pub trait ErrorCode {
66    /// Return the error code corresponding to this instance.
67    fn error_code(&self) -> i32;
68}