mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
style: Use ArcSlice for quotes.
This saves the intermediate allocation. Differential Revision: https://phabricator.services.mozilla.com/D30546
This commit is contained in:
parent
bbc77e3977
commit
a109fbb7c8
9 changed files with 85 additions and 44 deletions
|
@ -93,6 +93,7 @@ pub mod values;
|
|||
#[macro_use]
|
||||
pub mod viewport;
|
||||
pub mod owned_slice;
|
||||
pub mod owned_str;
|
||||
|
||||
pub use crate::specified_value_info::{CssType, KeywordsCollectFn, SpecifiedValueInfo};
|
||||
pub use crate::values::{
|
||||
|
|
53
components/style_traits/owned_str.rs
Normal file
53
components/style_traits/owned_str.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
/* 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 https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#![allow(unsafe_code)]
|
||||
|
||||
//! A replacement for `Box<str>` that has a defined layout for FFI.
|
||||
|
||||
use crate::owned_slice::OwnedSlice;
|
||||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
/// A struct that basically replaces a Box<str>, but with a defined layout,
|
||||
/// suitable for FFI.
|
||||
#[repr(C)]
|
||||
#[derive(Default, Clone, PartialEq, Eq, MallocSizeOf, ToShmem)]
|
||||
pub struct OwnedStr(OwnedSlice<u8>);
|
||||
|
||||
impl fmt::Debug for OwnedStr {
|
||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.deref().fmt(formatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for OwnedStr {
|
||||
type Target = str;
|
||||
|
||||
#[inline(always)]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { std::str::from_utf8_unchecked(&*self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for OwnedStr {
|
||||
#[inline(always)]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
unsafe { std::str::from_utf8_unchecked_mut(&mut *self.0) }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Box<str>> for OwnedStr {
|
||||
#[inline]
|
||||
fn from(b: Box<str>) -> Self {
|
||||
Self::from(b.into_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for OwnedStr {
|
||||
#[inline]
|
||||
fn from(s: String) -> Self {
|
||||
OwnedStr(s.into_bytes().into())
|
||||
}
|
||||
}
|
|
@ -84,6 +84,7 @@ impl SpecifiedValueInfo for u16 {}
|
|||
impl SpecifiedValueInfo for u32 {}
|
||||
impl SpecifiedValueInfo for str {}
|
||||
impl SpecifiedValueInfo for String {}
|
||||
impl SpecifiedValueInfo for crate::owned_str::OwnedStr {}
|
||||
|
||||
#[cfg(feature = "servo")]
|
||||
impl SpecifiedValueInfo for ::servo_atoms::Atom {}
|
||||
|
|
|
@ -75,6 +75,16 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl ToCss for crate::owned_str::OwnedStr {
|
||||
#[inline]
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
serialize_string(self, dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToCss for str {
|
||||
#[inline]
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue