From 6d6d35f2e06d100dfdeae43fc168cd193d35d74c Mon Sep 17 00:00:00 2001 From: Pantonshire Date: Fri, 22 Jul 2022 17:46:45 +0100 Subject: [PATCH] More conversion functions for InliningString --- src/strings/experimental.rs | 39 +++++++++++++++++++++++++++++++++- src/strings/inlining.rs | 42 ++++++++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/strings/experimental.rs b/src/strings/experimental.rs index 15d435f..a00727a 100644 --- a/src/strings/experimental.rs +++ b/src/strings/experimental.rs @@ -16,11 +16,17 @@ use core::{ use alloc::{ borrow::Cow, boxed::Box, + rc::Rc, string::String, + sync::Arc, }; #[cfg(feature = "std")] -use std::borrow::Cow; +use std::{ + borrow::Cow, + rc::Rc, + sync::Arc, +}; /// A non-growable string where strings 23 bytes or shorter are stored inline and longer strings /// use a separate heap allocation. If maximum inline lengths other than 23 are desired, see the @@ -455,6 +461,13 @@ impl From for InliningString { } } +impl From> for InliningString { + #[inline] + fn from(s: Box) -> Self { + Self::new(s) + } +} + impl<'a, const N: usize> From> for InliningString { #[inline] fn from(s: Cow<'a, str>) -> Self { @@ -469,6 +482,27 @@ impl From> for String { } } +impl From> for Box { + #[inline] + fn from(s: InliningString) -> Self { + s.into_boxed_str() + } +} + +impl From> for Rc { + #[inline] + fn from(s: InliningString) -> Self { + Rc::from(s.into_boxed_str()) + } +} + +impl From> for Arc { + #[inline] + fn from(s: InliningString) -> Self { + Arc::from(s.into_boxed_str()) + } +} + impl PartialEq> for InliningString { #[inline] fn eq(&self, other: &InliningString) -> bool { @@ -527,6 +561,9 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString { where D: serde::Deserializer<'de> { + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; + use serde::de::{Error, Unexpected, Visitor}; struct InliningStringVisitor; diff --git a/src/strings/inlining.rs b/src/strings/inlining.rs index a7dc1d3..c854d0b 100644 --- a/src/strings/inlining.rs +++ b/src/strings/inlining.rs @@ -10,14 +10,19 @@ use core::{ #[cfg(not(feature = "std"))] use alloc::{ - borrow::{Cow, ToOwned}, + borrow::Cow, boxed::Box, string::String, - vec::Vec, + rc::Rc, + sync::Arc, }; #[cfg(feature = "std")] -use std::borrow::Cow; +use std::{ + borrow::Cow, + rc::Rc, + sync::Arc, +}; use super::CappedString; @@ -253,6 +258,13 @@ impl From for InliningString { } } +impl From> for InliningString { + #[inline] + fn from(s: Box) -> Self { + Self::new(s) + } +} + impl<'a, const N: usize> From> for InliningString { #[inline] fn from(s: Cow<'a, str>) -> Self { @@ -267,6 +279,27 @@ impl From> for String { } } +impl From> for Box { + #[inline] + fn from(s: InliningString) -> Self { + s.into_boxed_str() + } +} + +impl From> for Rc { + #[inline] + fn from(s: InliningString) -> Self { + Rc::from(s.into_boxed_str()) + } +} + +impl From> for Arc { + #[inline] + fn from(s: InliningString) -> Self { + Arc::from(s.into_boxed_str()) + } +} + impl PartialEq> for InliningString { #[inline] fn eq(&self, other: &InliningString) -> bool { @@ -336,6 +369,9 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString { where D: serde::Deserializer<'de>, { + #[cfg(not(feature = "std"))] + use alloc::vec::Vec; + use serde::de::{Error, Unexpected, Visitor}; struct InliningStringVisitor;