tor-browser

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

ownership.rs (1632B)


      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 //! Helpers for different FFI pointer kinds that Gecko's FFI layer uses.
      6 
      7 use crate::gecko_bindings::structs::root::mozilla::detail::CopyablePtr;
      8 use servo_arc::Arc;
      9 use std::marker::PhantomData;
     10 use std::ops::{Deref, DerefMut};
     11 use std::ptr;
     12 
     13 /// Gecko-FFI-safe Arc (T is an ArcInner).
     14 ///
     15 /// This can be null.
     16 ///
     17 /// Leaks on drop. Please don't drop this.
     18 #[repr(C)]
     19 pub struct Strong<GeckoType> {
     20    ptr: *const GeckoType,
     21    _marker: PhantomData<GeckoType>,
     22 }
     23 
     24 impl<T> From<Arc<T>> for Strong<T> {
     25    fn from(arc: Arc<T>) -> Self {
     26        Self {
     27            ptr: Arc::into_raw(arc),
     28            _marker: PhantomData,
     29        }
     30    }
     31 }
     32 
     33 impl<T> From<Option<Arc<T>>> for Strong<T> {
     34    fn from(arc: Option<Arc<T>>) -> Self {
     35        match arc {
     36            Some(arc) => arc.into(),
     37            None => Self::null(),
     38        }
     39    }
     40 }
     41 
     42 impl<GeckoType> Strong<GeckoType> {
     43    #[inline]
     44    /// Returns whether this reference is null.
     45    pub fn is_null(&self) -> bool {
     46        self.ptr.is_null()
     47    }
     48 
     49    #[inline]
     50    /// Returns a null pointer
     51    pub fn null() -> Self {
     52        Self {
     53            ptr: ptr::null(),
     54            _marker: PhantomData,
     55        }
     56    }
     57 }
     58 
     59 impl<T> Deref for CopyablePtr<T> {
     60    type Target = T;
     61    fn deref(&self) -> &Self::Target {
     62        &self.mPtr
     63    }
     64 }
     65 
     66 impl<T> DerefMut for CopyablePtr<T> {
     67    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
     68        &mut self.mPtr
     69    }
     70 }