tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

lib.rs (1593B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 #[doc(hidden)]
      6 pub use bincode as _bincode;
      7 
      8 /// Takes a type, a serializer, and a deserializer name. The type must implement serde::Serialize.
      9 /// Defines a pair of ffi functions that go along with the MOZ_DEFINE_RUST_PARAMTRAITS macro.
     10 #[macro_export]
     11 macro_rules! define_ffi_serializer {
     12    ($ty:ty, $serializer:ident, $deserializer:ident) => {
     13        #[no_mangle]
     14        pub extern "C" fn $serializer(
     15            v: &$ty,
     16            out_len: &mut usize,
     17            out_cap: &mut usize,
     18        ) -> *mut u8 {
     19            *out_len = 0;
     20            *out_cap = 0;
     21            let mut buf = match $crate::_bincode::serialize(v) {
     22                Ok(buf) => buf,
     23                Err(..) => return std::ptr::null_mut(),
     24            };
     25            *out_len = buf.len();
     26            *out_cap = buf.capacity();
     27            let ptr = buf.as_mut_ptr();
     28            std::mem::forget(buf);
     29            ptr
     30        }
     31 
     32        #[no_mangle]
     33        pub unsafe extern "C" fn $deserializer(
     34            input: *const u8,
     35            len: usize,
     36            out: *mut $ty,
     37        ) -> bool {
     38            let slice = std::slice::from_raw_parts(input, len);
     39            let element = match $crate::_bincode::deserialize(slice) {
     40                Ok(buf) => buf,
     41                Err(..) => return false,
     42            };
     43            std::ptr::write(out, element);
     44            true
     45        }
     46    };
     47 }