smol_bytes/utf8_bytes_mut/
from.rs1use core::{convert::Infallible, str::FromStr};
2use std::string::String;
3
4use super::*;
5
6impl From<&str> for Utf8BytesMut {
7 fn from(s: &str) -> Self {
8 Self {
9 inner: BytesMut::from(s.as_bytes()),
10 }
11 }
12}
13
14impl FromStr for Utf8BytesMut {
15 type Err = Infallible;
16
17 fn from_str(s: &str) -> Result<Self, Self::Err> {
18 Ok(Self::from(s))
19 }
20}
21
22impl From<String> for Utf8BytesMut {
23 fn from(s: String) -> Self {
24 Self {
25 inner: BytesMut::from(s.into_bytes()),
26 }
27 }
28}
29
30impl TryFrom<BytesMut> for Utf8BytesMut {
31 type Error = core::str::Utf8Error;
32
33 fn try_from(bytes: BytesMut) -> Result<Self, Self::Error> {
34 core::str::from_utf8(bytes.as_ref())?;
35 Ok(Self { inner: bytes })
36 }
37}
38
39impl TryFrom<&[u8]> for Utf8BytesMut {
40 type Error = core::str::Utf8Error;
41
42 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
44 let s = core::str::from_utf8(bytes)?;
45 Ok(Self::from(s))
46 }
47}
48
49impl From<Utf8BytesMut> for BytesMut {
50 fn from(value: Utf8BytesMut) -> Self {
51 value.inner
52 }
53}
54
55impl From<Utf8BytesMut> for String {
56 fn from(value: Utf8BytesMut) -> Self {
57 String::from_utf8(value.inner.into()).expect("Utf8Bytes invariant")
58 }
59}
60
61#[cfg(any(feature = "std", feature = "alloc"))]
62const _: () = {
63 use std::{boxed::Box, rc::Rc, sync::Arc};
64
65 impl From<Utf8BytesMut> for Box<str> {
66 fn from(value: Utf8BytesMut) -> Self {
67 value.as_str().into()
68 }
69 }
70
71 impl From<Utf8BytesMut> for Arc<str> {
72 fn from(value: Utf8BytesMut) -> Self {
73 value.as_str().into()
74 }
75 }
76
77 impl From<Utf8BytesMut> for Rc<str> {
78 fn from(value: Utf8BytesMut) -> Self {
79 value.as_str().into()
80 }
81 }
82
83 impl From<&String> for Utf8BytesMut {
84 fn from(s: &String) -> Self {
85 Self::from(s.as_str())
86 }
87 }
88
89 impl From<Box<str>> for Utf8BytesMut {
90 fn from(s: Box<str>) -> Self {
91 Self::from(s.as_ref())
92 }
93 }
94
95 impl From<Arc<str>> for Utf8BytesMut {
96 fn from(s: Arc<str>) -> Self {
97 Self::from(s.as_ref())
98 }
99 }
100
101 impl From<Rc<str>> for Utf8BytesMut {
102 fn from(s: Rc<str>) -> Self {
103 Self::from(s.as_ref())
104 }
105 }
106};