More conversion functions for InliningString
This commit is contained in:
1 parent
f72b53e84f
commit
6d6d35f2e0
2 files changed
+77
-4
No files matched your search
@@ -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<const N: usize> From<String> for InliningString<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<Box<str>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn from(s: Box<str>) -> Self {
|
||||
Self::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, const N: usize> From<Cow<'a, str>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn from(s: Cow<'a, str>) -> Self {
|
||||
@@ -469,6 +482,27 @@ impl<const N: usize> From<InliningString<N>> for String {
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Box<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
s.into_boxed_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Rc<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
Rc::from(s.into_boxed_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Arc<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
Arc::from(s.into_boxed_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, const M: usize> PartialEq<InliningString<M>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &InliningString<M>) -> bool {
|
||||
@@ -527,6 +561,9 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString<N> {
|
||||
where
|
||||
D: serde::Deserializer<'de>
|
||||
{
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use serde::de::{Error, Unexpected, Visitor};
|
||||
|
||||
struct InliningStringVisitor<const N: usize>;
|
||||
|
||||
+39
-3
@@ -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<const N: usize> From<String> for InliningString<N> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<Box<str>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn from(s: Box<str>) -> Self {
|
||||
Self::new(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, const N: usize> From<Cow<'a, str>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn from(s: Cow<'a, str>) -> Self {
|
||||
@@ -267,6 +279,27 @@ impl<const N: usize> From<InliningString<N>> for String {
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Box<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
s.into_boxed_str()
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Rc<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
Rc::from(s.into_boxed_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize> From<InliningString<N>> for Arc<str> {
|
||||
#[inline]
|
||||
fn from(s: InliningString<N>) -> Self {
|
||||
Arc::from(s.into_boxed_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, const M: usize> PartialEq<InliningString<M>> for InliningString<N> {
|
||||
#[inline]
|
||||
fn eq(&self, other: &InliningString<M>) -> bool {
|
||||
@@ -336,6 +369,9 @@ impl<'de, const N: usize> serde::Deserialize<'de> for InliningString<N> {
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
#[cfg(not(feature = "std"))]
|
||||
use alloc::vec::Vec;
|
||||
|
||||
use serde::de::{Error, Unexpected, Visitor};
|
||||
|
||||
struct InliningStringVisitor<const N: usize>;
|
||||
|
||||
Reference in new issue
Block a user