devela/text/grapheme/
string_u8.rs
1use super::Grapheme;
10#[cfg(_char··)]
11use crate::text::char::*;
12#[cfg(feature = "alloc")]
13use crate::CString;
14use crate::{unwrap, ConstDefault, IterChars, MismatchedCapacity, StringU8};
15#[derive(Clone, PartialEq, Eq, Hash)]
22#[repr(transparent)]
23#[must_use]
24pub struct GraphemeU8<const CAP: usize>(StringU8<CAP>);
25
26impl<const CAP: usize> GraphemeU8<CAP> {
27 pub const fn new() -> Result<Self, MismatchedCapacity> {
32 Ok(Self(unwrap![ok? StringU8::new()]))
33 }
34
35 #[cfg(feature = "_char7")]
42 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char7")))]
43 pub const fn from_char7(c: char7) -> Result<Self, MismatchedCapacity> {
44 Ok(Self(unwrap![ok? StringU8::from_char7(c)]))
45 }
46
47 #[cfg(feature = "_char8")]
55 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char8")))]
56 pub const fn from_char8(c: char8) -> Result<Self, MismatchedCapacity> {
57 Ok(Self(unwrap![ok? StringU8::from_char8(c)]))
58 }
59
60 #[cfg(feature = "_char16")]
68 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "_char16")))]
69 pub const fn from_char16(c: char16) -> Result<Self, MismatchedCapacity> {
70 Ok(Self(unwrap![ok? StringU8::from_char16(c)]))
71 }
72
73 pub const fn from_char(c: char) -> Result<Self, MismatchedCapacity> {
80 Ok(Self(unwrap![ok? StringU8::from_char(c)]))
81 }
82
83 #[must_use] #[rustfmt::skip]
87 pub const fn len(&self) -> usize { self.0.len() }
88
89 #[must_use] #[rustfmt::skip]
91 pub const fn is_empty(&self) -> bool { self.0.len() == 0 }
92
93 #[must_use] #[rustfmt::skip]
95 pub const fn capacity() -> usize { CAP }
96
97 #[must_use] #[rustfmt::skip]
99 pub const fn remaining_capacity(&self) -> usize { CAP - self.len() }
100
101 #[must_use] #[rustfmt::skip]
103 pub const fn is_full(&self) -> bool { self.len() == CAP }
104
105 #[rustfmt::skip]
107 pub fn clear(&mut self) { self.0.clear(); }
108
109 #[must_use] #[rustfmt::skip]
113 pub const fn as_bytes(&self) -> &[u8] { self.0.as_bytes() }
114
115 #[must_use]
119 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_slice"))]
120 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
121 pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8] {
122 unsafe { self.0.as_bytes_mut() }
124 }
125
126 #[must_use] #[rustfmt::skip]
130 pub const fn as_array(&self) -> [u8; CAP] { self.0.as_array() }
131
132 #[must_use] #[rustfmt::skip]
136 pub const fn into_array(self) -> [u8; CAP] { self.0.into_array() }
137
138 #[must_use] #[rustfmt::skip]
140 pub const fn as_str(&self) -> &str { self.0.as_str() }
141
142 #[cfg(all(not(feature = "safe_text"), feature = "unsafe_slice"))]
146 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "unsafe_slice")))]
147 pub unsafe fn as_mut_str(&mut self) -> &mut str {
148 self.0.as_mut_str()
149 }
150
151 #[rustfmt::skip]
153 pub fn chars(&self) -> IterChars { self.0.chars() }
154
155 #[rustfmt::skip]
157 #[cfg(feature = "alloc")]
158 #[cfg_attr(feature = "nightly_doc", doc(cfg(feature = "alloc")))]
159 pub fn to_cstring(&self) -> CString { self.0.to_cstring() }
160}
161
162impl<const CAP: usize> Grapheme for GraphemeU8<CAP> {}
165
166mod core_impls {
167 use super::*;
168 use core::fmt;
169
170 impl<const CAP: usize> Default for GraphemeU8<CAP> {
171 #[rustfmt::skip]
176 fn default() -> Self { unwrap![ok Self::new()] }
177 }
178 impl<const CAP: usize> ConstDefault for GraphemeU8<CAP> {
179 const DEFAULT: Self = unwrap![ok Self::new()];
184 }
185
186 impl<const CAP: usize> fmt::Display for GraphemeU8<CAP> {
187 #[rustfmt::skip]
188 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) }
189 }
190 impl<const CAP: usize> fmt::Debug for GraphemeU8<CAP> {
191 #[rustfmt::skip]
192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.0) }
193 }
194
195 }