Add bindings for growing nsTArray

This commit is contained in:
Manish Goregaokar 2016-07-05 16:56:53 +05:30
parent 38b57c435d
commit 154bfb0b0f
3 changed files with 22 additions and 1 deletions

View file

@ -243,6 +243,8 @@ extern "C" {
pub fn Gecko_GetNodeFlags(node: *mut RawGeckoNode) -> u32;
pub fn Gecko_SetNodeFlags(node: *mut RawGeckoNode, flags: u32);
pub fn Gecko_UnsetNodeFlags(node: *mut RawGeckoNode, flags: u32);
pub fn Gecko_ArrayEnsureCapacity(array: *mut ::std::os::raw::c_void,
capacity: usize, size: usize);
pub fn Servo_StylesheetFromUTF8Bytes(bytes: *const u8, length: u32,
parsing_mode: SheetParsingMode,
base: *mut ThreadSafeURIHolder,

View file

@ -2,8 +2,10 @@
* 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 bindings::Gecko_ArrayEnsureCapacity;
use std::mem;
use std::ops::{Deref, DerefMut};
use std::os::raw::c_void;
use std::slice;
use structs::{nsTArray, nsTArrayHeader};
@ -33,10 +35,27 @@ impl<T> nsTArray<T> {
debug_assert!(!self.mBuffer.is_null());
unsafe { mem::transmute(self.mBuffer) }
}
fn header_mut <'a>(&'a mut self) -> &'a mut nsTArrayHeader {
debug_assert!(!self.mBuffer.is_null());
unsafe { 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 _
}
fn ensure_capacity(&mut self, cap: usize) {
unsafe {
Gecko_ArrayEnsureCapacity(self as *mut nsTArray<T> as *mut c_void, cap, mem::size_of::<T>())
}
}
// unsafe because the array may contain uninits
pub unsafe fn set_len(&mut self, len: u32) {
self.ensure_capacity(len as usize);
let mut header = self.header_mut();
header.mLength = len;
}
}