Format components fallible and geometry #21373

This commit is contained in:
kingdido999 2018-09-04 09:51:55 +08:00
parent 156b1cc891
commit ec9d0f21a6
2 changed files with 36 additions and 33 deletions

View file

@ -18,7 +18,6 @@ pub trait FallibleVec<T> {
fn try_push(&mut self, value: T) -> Result<(), FailedAllocationError>; fn try_push(&mut self, value: T) -> Result<(), FailedAllocationError>;
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// Vec // Vec
@ -52,14 +51,14 @@ fn try_double_vec<T>(vec: &mut Vec<T>) -> Result<(), FailedAllocationError> {
let new_cap: usize = if old_cap == 0 { let new_cap: usize = if old_cap == 0 {
4 4
} else { } else {
old_cap.checked_mul(2).ok_or(FailedAllocationError::new( old_cap
"capacity overflow for Vec", .checked_mul(2)
))? .ok_or(FailedAllocationError::new("capacity overflow for Vec"))?
}; };
let new_size_bytes = new_cap.checked_mul(mem::size_of::<T>()).ok_or( let new_size_bytes = new_cap
FailedAllocationError::new("capacity overflow for Vec"), .checked_mul(mem::size_of::<T>())
)?; .ok_or(FailedAllocationError::new("capacity overflow for Vec"))?;
let new_ptr = unsafe { let new_ptr = unsafe {
if old_cap == 0 { if old_cap == 0 {
@ -75,15 +74,12 @@ fn try_double_vec<T>(vec: &mut Vec<T>) -> Result<(), FailedAllocationError> {
)); ));
} }
let new_vec = unsafe { let new_vec = unsafe { Vec::from_raw_parts(new_ptr as *mut T, old_len, new_cap) };
Vec::from_raw_parts(new_ptr as *mut T, old_len, new_cap)
};
mem::forget(mem::replace(vec, new_vec)); mem::forget(mem::replace(vec, new_vec));
Ok(()) Ok(())
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// SmallVec // SmallVec
@ -107,8 +103,7 @@ impl<T: Array> FallibleVec<T::Item> for SmallVec<T> {
#[cfg(feature = "known_system_malloc")] #[cfg(feature = "known_system_malloc")]
#[inline(never)] #[inline(never)]
#[cold] #[cold]
fn try_double_small_vec<T>(svec: &mut SmallVec<T>) fn try_double_small_vec<T>(svec: &mut SmallVec<T>) -> Result<(), FailedAllocationError>
-> Result<(), FailedAllocationError>
where where
T: Array, T: Array,
{ {
@ -122,20 +117,20 @@ where
let new_cap: usize = if old_cap == 0 { let new_cap: usize = if old_cap == 0 {
4 4
} else { } else {
old_cap.checked_mul(2).ok_or(FailedAllocationError::new( old_cap
"capacity overflow for SmallVec", .checked_mul(2)
))? .ok_or(FailedAllocationError::new("capacity overflow for SmallVec"))?
}; };
// This surely shouldn't fail, if |old_cap| was previously accepted as a // This surely shouldn't fail, if |old_cap| was previously accepted as a
// valid value. But err on the side of caution. // valid value. But err on the side of caution.
let old_size_bytes = old_cap.checked_mul(mem::size_of::<T>()).ok_or( let old_size_bytes = old_cap
FailedAllocationError::new("capacity overflow for SmallVec"), .checked_mul(mem::size_of::<T>())
)?; .ok_or(FailedAllocationError::new("capacity overflow for SmallVec"))?;
let new_size_bytes = new_cap.checked_mul(mem::size_of::<T>()).ok_or( let new_size_bytes = new_cap
FailedAllocationError::new("capacity overflow for SmallVec"), .checked_mul(mem::size_of::<T>())
)?; .ok_or(FailedAllocationError::new("capacity overflow for SmallVec"))?;
let new_ptr; let new_ptr;
if svec.spilled() { if svec.spilled() {
@ -149,8 +144,7 @@ where
unsafe { unsafe {
new_ptr = alloc::alloc(new_size_bytes, 0); new_ptr = alloc::alloc(new_size_bytes, 0);
if !new_ptr.is_null() && old_size_bytes > 0 { if !new_ptr.is_null() && old_size_bytes > 0 {
copy_nonoverlapping(old_ptr as *const u8, copy_nonoverlapping(old_ptr as *const u8, new_ptr as *mut u8, old_size_bytes);
new_ptr as *mut u8, old_size_bytes);
} }
} }
} }
@ -161,9 +155,7 @@ where
)); ));
} }
let new_vec = unsafe { let new_vec = unsafe { Vec::from_raw_parts(new_ptr as *mut T::Item, old_len, new_cap) };
Vec::from_raw_parts(new_ptr as *mut T::Item, old_len, new_cap)
};
let new_svec = SmallVec::from_vec(new_vec); let new_svec = SmallVec::from_vec(new_vec);
mem::forget(mem::replace(svec, new_svec)); mem::forget(mem::replace(svec, new_svec));

View file

@ -6,7 +6,8 @@ extern crate app_units;
extern crate euclid; extern crate euclid;
extern crate malloc_size_of; extern crate malloc_size_of;
extern crate style_traits; extern crate style_traits;
#[macro_use] extern crate malloc_size_of_derive; #[macro_use]
extern crate malloc_size_of_derive;
extern crate webrender_api; extern crate webrender_api;
use app_units::{Au, MAX_AU, MIN_AU}; use app_units::{Au, MAX_AU, MIN_AU};
@ -48,7 +49,7 @@ impl MaxRect for Rect<Au> {
fn max_rect() -> Rect<Au> { fn max_rect() -> Rect<Au> {
Rect::new( Rect::new(
Point2D::new(MIN_AU / 2, MIN_AU / 2), Point2D::new(MIN_AU / 2, MIN_AU / 2),
Size2D::new(MAX_AU, MAX_AU) Size2D::new(MAX_AU, MAX_AU),
) )
} }
} }
@ -64,12 +65,22 @@ impl MaxRect for LayoutRect {
/// A helper function to convert a rect of `f32` pixels to a rect of app units. /// A helper function to convert a rect of `f32` pixels to a rect of app units.
pub fn f32_rect_to_au_rect(rect: Rect<f32>) -> Rect<Au> { pub fn f32_rect_to_au_rect(rect: Rect<f32>) -> Rect<Au> {
Rect::new(Point2D::new(Au::from_f32_px(rect.origin.x), Au::from_f32_px(rect.origin.y)), Rect::new(
Size2D::new(Au::from_f32_px(rect.size.width), Au::from_f32_px(rect.size.height))) Point2D::new(
Au::from_f32_px(rect.origin.x),
Au::from_f32_px(rect.origin.y),
),
Size2D::new(
Au::from_f32_px(rect.size.width),
Au::from_f32_px(rect.size.height),
),
)
} }
/// A helper function to convert a rect of `Au` pixels to a rect of f32 units. /// A helper function to convert a rect of `Au` pixels to a rect of f32 units.
pub fn au_rect_to_f32_rect(rect: Rect<Au>) -> Rect<f32> { pub fn au_rect_to_f32_rect(rect: Rect<Au>) -> Rect<f32> {
Rect::new(Point2D::new(rect.origin.x.to_f32_px(), rect.origin.y.to_f32_px()), Rect::new(
Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px())) Point2D::new(rect.origin.x.to_f32_px(), rect.origin.y.to_f32_px()),
Size2D::new(rect.size.width.to_f32_px(), rect.size.height.to_f32_px()),
)
} }