mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Move gecko_bindings and gecko_string_cache into the style crate
This commit is contained in:
parent
0dd005eacc
commit
c6787458d9
33 changed files with 2629 additions and 2688 deletions
93
components/style/gecko_bindings/sugar/ns_t_array.rs
Normal file
93
components/style/gecko_bindings/sugar/ns_t_array.rs
Normal file
|
@ -0,0 +1,93 @@
|
|||
/* 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/. */
|
||||
|
||||
use gecko_bindings::bindings;
|
||||
use std::mem;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
use std::os::raw::c_void;
|
||||
use std::slice;
|
||||
use gecko_bindings::structs::{nsTArray, nsTArrayHeader};
|
||||
|
||||
impl<T> Deref for nsTArray<T> {
|
||||
type Target = [T];
|
||||
|
||||
fn deref<'a>(&'a self) -> &'a [T] {
|
||||
unsafe {
|
||||
slice::from_raw_parts(self.slice_begin(),
|
||||
self.header().mLength as usize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for nsTArray<T> {
|
||||
fn deref_mut<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe {
|
||||
slice::from_raw_parts_mut(self.slice_begin(),
|
||||
self.header().mLength as usize)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> nsTArray<T> {
|
||||
#[inline]
|
||||
fn header<'a>(&'a self) -> &'a nsTArrayHeader {
|
||||
debug_assert!(!self.mBuffer.is_null());
|
||||
unsafe { mem::transmute(self.mBuffer) }
|
||||
}
|
||||
// unsafe, since header may be in shared static or something
|
||||
unsafe fn header_mut<'a>(&'a mut self) -> &'a mut nsTArrayHeader {
|
||||
debug_assert!(!self.mBuffer.is_null());
|
||||
|
||||
mem::transmute(self.mBuffer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn slice_begin(&self) -> *mut T {
|
||||
debug_assert!(!self.mBuffer.is_null());
|
||||
(self.mBuffer as *const nsTArrayHeader).offset(1) as *mut _
|
||||
}
|
||||
|
||||
/// Ensures the array has enough capacity at least to hold `cap` elements.
|
||||
///
|
||||
/// NOTE: This doesn't call the constructor on the values!
|
||||
pub fn ensure_capacity(&mut self, cap: usize) {
|
||||
if cap >= self.len() {
|
||||
unsafe {
|
||||
bindings::Gecko_EnsureTArrayCapacity(self as *mut nsTArray<T> as *mut _,
|
||||
cap, mem::size_of::<T>())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Clears the array storage without calling the destructor on the values.
|
||||
#[inline]
|
||||
pub unsafe fn clear(&mut self) {
|
||||
if self.len() != 0 {
|
||||
bindings::Gecko_ClearPODTArray(self as *mut nsTArray<T> as *mut _,
|
||||
mem::size_of::<T>(),
|
||||
mem::align_of::<T>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Clears a POD array. This is safe since copy types are memcopyable.
|
||||
#[inline]
|
||||
pub fn clear_pod(&mut self)
|
||||
where T: Copy
|
||||
{
|
||||
unsafe { self.clear() }
|
||||
}
|
||||
|
||||
// unsafe because the array may contain uninits
|
||||
// This will not call constructors, either manually
|
||||
// add bindings or run the typed ensurecapacity call
|
||||
// on the gecko side
|
||||
pub unsafe fn set_len(&mut self, len: u32) {
|
||||
// this can leak
|
||||
debug_assert!(len >= self.len() as u32);
|
||||
self.ensure_capacity(len as usize);
|
||||
let mut header = self.header_mut();
|
||||
header.mLength = len;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue