mirror of
https://github.com/servo/servo.git
synced 2025-07-12 18:03:49 +01:00
In the Gecko case, this style source would be the style context. In the servo case, it will be always the computed values. We could optimise this further in the case of stylo (from three FFI calls to one) if we use an API of the form CalcAndStore(node, new_cv). But that would imply borrowing the data twice from Servo (we also have borrow_data_unchecked fwiw, but...).
65 lines
1.8 KiB
Rust
65 lines
1.8 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#![allow(unsafe_code)]
|
|
|
|
use std::marker::PhantomData;
|
|
use std::mem::{forget, transmute};
|
|
use std::sync::Arc;
|
|
|
|
pub struct ArcHelpers<GeckoType, ServoType> {
|
|
phantom1: PhantomData<GeckoType>,
|
|
phantom2: PhantomData<ServoType>,
|
|
}
|
|
|
|
impl<GeckoType, ServoType> ArcHelpers<GeckoType, ServoType> {
|
|
pub fn with<F, Output>(raw: *mut GeckoType, cb: F) -> Output
|
|
where F: FnOnce(&Arc<ServoType>) -> Output {
|
|
debug_assert!(!raw.is_null());
|
|
|
|
let owned = unsafe { Self::into(raw) };
|
|
let result = cb(&owned);
|
|
forget(owned);
|
|
result
|
|
}
|
|
|
|
pub fn maybe_with<F, Output>(maybe_raw: *mut GeckoType, cb: F) -> Output
|
|
where F: FnOnce(Option<&Arc<ServoType>>) -> Output {
|
|
let owned = if maybe_raw.is_null() {
|
|
None
|
|
} else {
|
|
Some(unsafe { Self::into(maybe_raw) })
|
|
};
|
|
|
|
let result = cb(owned.as_ref());
|
|
forget(owned);
|
|
|
|
result
|
|
}
|
|
|
|
pub unsafe fn into(ptr: *mut GeckoType) -> Arc<ServoType> {
|
|
transmute(ptr)
|
|
}
|
|
|
|
pub fn from(owned: Arc<ServoType>) -> *mut GeckoType {
|
|
unsafe { transmute(owned) }
|
|
}
|
|
|
|
pub fn borrow<F, Output>(borrowed: &Arc<ServoType>, cb: F) -> Output
|
|
where F: FnOnce(&mut GeckoType) -> Output
|
|
{
|
|
let borrowed_gecko_type: *const &mut GeckoType =
|
|
unsafe { transmute(borrowed) };
|
|
|
|
unsafe { cb(*borrowed_gecko_type) }
|
|
}
|
|
|
|
pub unsafe fn addref(ptr: *mut GeckoType) {
|
|
Self::with(ptr, |arc| forget(arc.clone()));
|
|
}
|
|
|
|
pub unsafe fn release(ptr: *mut GeckoType) {
|
|
let _ = Self::into(ptr);
|
|
}
|
|
}
|