aboutsummaryrefslogtreecommitdiffstats
path: root/components/style_traits
diff options
context:
space:
mode:
Diffstat (limited to 'components/style_traits')
-rw-r--r--components/style_traits/arc_slice.rs21
-rw-r--r--components/style_traits/owned_slice.rs21
-rw-r--r--components/style_traits/owned_str.rs28
3 files changed, 70 insertions, 0 deletions
diff --git a/components/style_traits/arc_slice.rs b/components/style_traits/arc_slice.rs
index bbbac1a0757..f5d0c56e7fc 100644
--- a/components/style_traits/arc_slice.rs
+++ b/components/style_traits/arc_slice.rs
@@ -4,6 +4,8 @@
//! A thin atomically-reference-counted slice.
+use serde::de::{Deserialize, Deserializer};
+use serde::ser::{Serialize, Serializer};
use servo_arc::ThinArc;
use std::ops::Deref;
use std::ptr::NonNull;
@@ -60,6 +62,25 @@ impl<T> Default for ArcSlice<T> {
}
}
+impl<T: Serialize> Serialize for ArcSlice<T> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.deref().serialize(serializer)
+ }
+}
+
+impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArcSlice<T> {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let r = Vec::deserialize(deserializer)?;
+ Ok(ArcSlice::from_iter(r.into_iter()))
+ }
+}
+
impl<T> ArcSlice<T> {
/// Creates an Arc for a slice using the given iterator to generate the
/// slice.
diff --git a/components/style_traits/owned_slice.rs b/components/style_traits/owned_slice.rs
index 33d3ac1c2ab..bbce7065196 100644
--- a/components/style_traits/owned_slice.rs
+++ b/components/style_traits/owned_slice.rs
@@ -7,6 +7,8 @@
//! A replacement for `Box<[T]>` that cbindgen can understand.
use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
+use serde::de::{Deserialize, Deserializer};
+use serde::ser::{Serialize, Serializer};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::ptr::NonNull;
@@ -171,3 +173,22 @@ impl<T> iter::FromIterator<T> for OwnedSlice<T> {
Vec::from_iter(iter).into()
}
}
+
+impl<T: Serialize> Serialize for OwnedSlice<T> {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: Serializer,
+ {
+ self.deref().serialize(serializer)
+ }
+}
+
+impl<'de, T: Deserialize<'de>> Deserialize<'de> for OwnedSlice<T> {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: Deserializer<'de>,
+ {
+ let r = Box::<[T]>::deserialize(deserializer)?;
+ Ok(r.into())
+ }
+}
diff --git a/components/style_traits/owned_str.rs b/components/style_traits/owned_str.rs
index 42a83a07713..ebfdcd5e066 100644
--- a/components/style_traits/owned_str.rs
+++ b/components/style_traits/owned_str.rs
@@ -38,6 +38,34 @@ impl DerefMut for OwnedStr {
}
}
+impl OwnedStr {
+ /// Convert the OwnedStr into a boxed str.
+ #[inline]
+ pub fn into_box(self) -> Box<str> {
+ self.into_string().into_boxed_str()
+ }
+
+ /// Convert the OwnedStr into a `String`.
+ #[inline]
+ pub fn into_string(self) -> String {
+ unsafe { String::from_utf8_unchecked(self.0.into_vec()) }
+ }
+}
+
+impl From<OwnedStr> for String {
+ #[inline]
+ fn from(b: OwnedStr) -> Self {
+ b.into_string()
+ }
+}
+
+impl From<OwnedStr> for Box<str> {
+ #[inline]
+ fn from(b: OwnedStr) -> Self {
+ b.into_box()
+ }
+}
+
impl From<Box<str>> for OwnedStr {
#[inline]
fn from(b: Box<str>) -> Self {