1#[cfg(not(any(feature = "std", feature = "alloc")))]
4compile_error!("expected either `std` or `alloc` to be enabled");
5
6#[cfg(feature = "std")]
7use std::{borrow::Cow, collections::TryReserveError, ffi::OsStr, path::Path};
8
9#[cfg(all(not(feature = "std"), feature = "alloc"))]
10use alloc::{
11 borrow::{Cow, ToOwned},
12 boxed::Box,
13 collections::TryReserveError,
14 string::{String, ToString},
15};
16
17use core::{
18 borrow::{Borrow, BorrowMut},
19 convert::Infallible,
20 fmt,
21 ops::{Add, AddAssign, Deref, DerefMut, RangeBounds},
22 str::FromStr,
23};
24
25use non_empty_iter::{FromNonEmptyIterator, IntoNonEmptyIterator, NonEmptyIterator};
26use non_empty_slice::{EmptyByteVec, EmptySlice, NonEmptyByteVec, NonEmptyBytes};
27use non_zero_size::Size;
28
29use thiserror::Error;
30
31use crate::{
32 boxed::{EmptyBoxedStr, NonEmptyBoxedStr},
33 cow::NonEmptyCowStr,
34 internal::{ByteVec, Bytes},
35 str::{EmptyStr, FromNonEmptyStr, NonEmptyStr, NonEmptyUtf8Error},
36};
37
38pub const EMPTY_STRING: &str = "the string is empty";
40
41#[derive(Debug, Error)]
43#[error("{EMPTY_STRING}")]
44#[cfg_attr(
45 feature = "diagnostics",
46 derive(miette::Diagnostic),
47 diagnostic(code(non_empty_str::string), help("make sure the string is non-empty"))
48)]
49pub struct EmptyString {
50 string: String,
51}
52
53impl EmptyString {
54 pub(crate) const fn new(string: String) -> Self {
56 Self { string }
57 }
58
59 #[must_use]
61 pub fn get(self) -> String {
62 self.string
63 }
64
65 #[must_use]
67 pub fn from_empty_boxed_str(empty: EmptyBoxedStr) -> Self {
68 Self::new(empty.get().into_string())
69 }
70
71 #[must_use]
73 pub fn into_empty_boxed_str(self) -> EmptyBoxedStr {
74 EmptyBoxedStr::from_empty_string(self)
75 }
76}
77
78#[derive(Debug, Error)]
80#[error("{error}")]
81#[cfg_attr(
82 feature = "diagnostics",
83 derive(miette::Diagnostic),
84 diagnostic(
85 code(non_empty_str::string::utf8),
86 help("make sure the bytes are valid UTF-8")
87 )
88)]
89pub struct FromNonEmptyUtf8Error {
90 #[source]
91 #[cfg_attr(feature = "diagnostics", diagnostic_source)]
92 error: NonEmptyUtf8Error,
93 bytes: NonEmptyByteVec,
94}
95
96impl FromNonEmptyUtf8Error {
97 pub(crate) const fn new(error: NonEmptyUtf8Error, bytes: NonEmptyByteVec) -> Self {
99 Self { error, bytes }
100 }
101
102 #[must_use]
104 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
105 self.bytes.as_non_empty_slice()
106 }
107
108 #[must_use]
110 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
111 self.bytes
112 }
113
114 #[must_use]
116 pub const fn non_empty_error(&self) -> NonEmptyUtf8Error {
117 self.error
118 }
119
120 #[must_use]
127 pub fn recover(self) -> (NonEmptyUtf8Error, NonEmptyByteVec) {
128 (self.non_empty_error(), self.into_non_empty_bytes())
129 }
130}
131
132#[derive(Debug, Error)]
134#[error(transparent)]
135#[cfg_attr(
136 feature = "diagnostics",
137 derive(miette::Diagnostic),
138 diagnostic(transparent)
139)]
140pub enum FromMaybeEmptyUtf8Error {
141 Empty(#[from] EmptyByteVec),
143 Utf8(#[from] FromNonEmptyUtf8Error),
145}
146
147#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
149#[repr(transparent)]
150pub struct NonEmptyString {
151 inner: String,
152}
153
154impl Clone for NonEmptyString {
155 fn clone(&self) -> Self {
156 unsafe { Self::new_unchecked(self.as_string().clone()) }
158 }
159
160 fn clone_from(&mut self, source: &Self) {
161 unsafe {
163 self.as_mut_string().clone_from(source.as_string());
164 }
165 }
166}
167
168impl fmt::Write for NonEmptyString {
169 fn write_str(&mut self, string: &str) -> fmt::Result {
170 unsafe { self.as_mut_string().write_str(string) }
172 }
173
174 fn write_char(&mut self, character: char) -> fmt::Result {
175 unsafe { self.as_mut_string().write_char(character) }
177 }
178
179 fn write_fmt(&mut self, arguments: fmt::Arguments<'_>) -> fmt::Result {
180 unsafe { self.as_mut_string().write_fmt(arguments) }
182 }
183}
184
185impl fmt::Display for NonEmptyString {
186 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
187 self.as_string().fmt(formatter)
188 }
189}
190
191impl FromStr for NonEmptyString {
192 type Err = EmptyStr;
193
194 fn from_str(string: &str) -> Result<Self, Self::Err> {
195 NonEmptyStr::try_from_str(string).map(Self::from_non_empty_str)
196 }
197}
198
199impl FromNonEmptyStr for NonEmptyString {
200 type Error = Infallible;
201
202 fn from_non_empty_str(string: &NonEmptyStr) -> Result<Self, Self::Error> {
203 Ok(Self::from_non_empty_str(string))
204 }
205}
206
207impl Borrow<NonEmptyStr> for NonEmptyString {
208 fn borrow(&self) -> &NonEmptyStr {
209 self.as_non_empty_str()
210 }
211}
212
213impl BorrowMut<NonEmptyStr> for NonEmptyString {
214 fn borrow_mut(&mut self) -> &mut NonEmptyStr {
215 self.as_non_empty_mut_str()
216 }
217}
218
219impl Borrow<str> for NonEmptyString {
220 fn borrow(&self) -> &str {
221 self.as_str()
222 }
223}
224
225impl BorrowMut<str> for NonEmptyString {
226 fn borrow_mut(&mut self) -> &mut str {
227 self.as_mut_str()
228 }
229}
230
231impl TryFrom<String> for NonEmptyString {
232 type Error = EmptyString;
233
234 fn try_from(string: String) -> Result<Self, Self::Error> {
235 Self::new(string)
236 }
237}
238
239impl From<NonEmptyString> for String {
240 fn from(non_empty: NonEmptyString) -> Self {
241 non_empty.into_string()
242 }
243}
244
245impl From<&NonEmptyStr> for NonEmptyString {
246 fn from(non_empty: &NonEmptyStr) -> Self {
247 non_empty.to_non_empty_string()
248 }
249}
250
251impl From<&mut NonEmptyStr> for NonEmptyString {
252 fn from(non_empty: &mut NonEmptyStr) -> Self {
253 non_empty.to_non_empty_string()
254 }
255}
256
257impl TryFrom<&str> for NonEmptyString {
258 type Error = EmptyStr;
259
260 fn try_from(string: &str) -> Result<Self, Self::Error> {
261 let non_empty_string: &NonEmptyStr = string.try_into()?;
262
263 Ok(non_empty_string.into())
264 }
265}
266
267impl TryFrom<&mut str> for NonEmptyString {
268 type Error = EmptyStr;
269
270 fn try_from(string: &mut str) -> Result<Self, Self::Error> {
271 let non_empty_string: &mut NonEmptyStr = string.try_into()?;
272
273 Ok(non_empty_string.into())
274 }
275}
276
277impl From<char> for NonEmptyString {
278 fn from(character: char) -> Self {
279 Self::single(character)
280 }
281}
282
283impl AsRef<Self> for NonEmptyString {
284 fn as_ref(&self) -> &Self {
285 self
286 }
287}
288
289impl AsMut<Self> for NonEmptyString {
290 fn as_mut(&mut self) -> &mut Self {
291 self
292 }
293}
294
295impl AsRef<String> for NonEmptyString {
296 fn as_ref(&self) -> &String {
297 self.as_string()
298 }
299}
300
301impl AsRef<NonEmptyStr> for NonEmptyString {
302 fn as_ref(&self) -> &NonEmptyStr {
303 self.as_non_empty_str()
304 }
305}
306
307impl AsMut<NonEmptyStr> for NonEmptyString {
308 fn as_mut(&mut self) -> &mut NonEmptyStr {
309 self.as_non_empty_mut_str()
310 }
311}
312
313impl AsRef<str> for NonEmptyString {
314 fn as_ref(&self) -> &str {
315 self.as_str()
316 }
317}
318
319impl AsMut<str> for NonEmptyString {
320 fn as_mut(&mut self) -> &mut str {
321 self.as_mut_str()
322 }
323}
324
325impl AsRef<Bytes> for NonEmptyString {
326 fn as_ref(&self) -> &Bytes {
327 self.as_bytes()
328 }
329}
330
331#[cfg(feature = "std")]
332impl AsRef<OsStr> for NonEmptyString {
333 fn as_ref(&self) -> &OsStr {
334 self.as_string().as_ref()
335 }
336}
337
338#[cfg(feature = "std")]
339impl AsRef<Path> for NonEmptyString {
340 fn as_ref(&self) -> &Path {
341 self.as_string().as_ref()
342 }
343}
344
345impl Deref for NonEmptyString {
346 type Target = NonEmptyStr;
347
348 fn deref(&self) -> &Self::Target {
349 self.as_non_empty_str()
350 }
351}
352
353impl DerefMut for NonEmptyString {
354 fn deref_mut(&mut self) -> &mut Self::Target {
355 self.as_non_empty_mut_str()
356 }
357}
358
359impl Add<&str> for NonEmptyString {
360 type Output = Self;
361
362 fn add(mut self, string: &str) -> Self::Output {
363 self.push_str(string);
364
365 self
366 }
367}
368
369impl Add<&NonEmptyStr> for NonEmptyString {
370 type Output = Self;
371
372 fn add(mut self, non_empty: &NonEmptyStr) -> Self::Output {
373 self.extend_from(non_empty);
374
375 self
376 }
377}
378
379impl AddAssign<&str> for NonEmptyString {
380 fn add_assign(&mut self, string: &str) {
381 self.push_str(string);
382 }
383}
384
385impl AddAssign<&NonEmptyStr> for NonEmptyString {
386 fn add_assign(&mut self, non_empty: &NonEmptyStr) {
387 self.extend_from(non_empty);
388 }
389}
390
391impl<'s> Extend<&'s str> for NonEmptyString {
392 fn extend<I: IntoIterator<Item = &'s str>>(&mut self, iterable: I) {
393 unsafe {
395 self.as_mut_string().extend(iterable);
396 }
397 }
398}
399
400impl<'s> Extend<&'s NonEmptyStr> for NonEmptyString {
401 fn extend<I: IntoIterator<Item = &'s NonEmptyStr>>(&mut self, iterable: I) {
402 self.extend(iterable.into_iter().map(NonEmptyStr::as_str));
403 }
404}
405
406impl<'c> Extend<&'c char> for NonEmptyString {
407 fn extend<I: IntoIterator<Item = &'c char>>(&mut self, iterable: I) {
408 unsafe {
410 self.as_mut_string().extend(iterable);
411 }
412 }
413}
414
415impl Extend<Box<str>> for NonEmptyString {
416 fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iterable: I) {
417 unsafe {
419 self.as_mut_string().extend(iterable);
420 }
421 }
422}
423
424impl Extend<NonEmptyBoxedStr> for NonEmptyString {
425 fn extend<I: IntoIterator<Item = NonEmptyBoxedStr>>(&mut self, iterable: I) {
426 self.extend(iterable.into_iter().map(NonEmptyStr::into_boxed_str));
427 }
428}
429
430impl<'s> Extend<Cow<'s, str>> for NonEmptyString {
431 fn extend<I: IntoIterator<Item = Cow<'s, str>>>(&mut self, iterable: I) {
432 unsafe {
434 self.as_mut_string().extend(iterable);
435 }
436 }
437}
438
439impl<'s> Extend<NonEmptyCowStr<'s>> for NonEmptyString {
440 fn extend<I: IntoIterator<Item = Cow<'s, NonEmptyStr>>>(&mut self, iterable: I) {
441 self.extend(iterable.into_iter().map(|non_empty| match non_empty {
442 Cow::Borrowed(string) => Cow::Borrowed(string.as_str()),
443 Cow::Owned(string) => Cow::Owned(string.into_string()),
444 }));
445 }
446}
447
448impl Extend<String> for NonEmptyString {
449 fn extend<I: IntoIterator<Item = String>>(&mut self, iterable: I) {
450 unsafe {
452 self.as_mut_string().extend(iterable);
453 }
454 }
455}
456
457impl Extend<Self> for NonEmptyString {
458 fn extend<I: IntoIterator<Item = Self>>(&mut self, iterable: I) {
459 self.extend(iterable.into_iter().map(Self::into_string));
460 }
461}
462
463impl Extend<char> for NonEmptyString {
464 fn extend<I: IntoIterator<Item = char>>(&mut self, iterable: I) {
465 unsafe {
467 self.as_mut_string().extend(iterable);
468 }
469 }
470}
471
472impl NonEmptyString {
473 pub const fn new(string: String) -> Result<Self, EmptyString> {
499 if string.is_empty() {
500 return Err(EmptyString::new(string));
501 }
502
503 Ok(unsafe { Self::new_unchecked(string) })
505 }
506
507 #[must_use]
513 pub const unsafe fn new_unchecked(inner: String) -> Self {
514 debug_assert!(!inner.is_empty());
515
516 Self { inner }
517 }
518
519 #[cfg(feature = "unsafe-assert")]
520 const fn assert_non_empty(&self) {
521 use core::hint::assert_unchecked;
522
523 unsafe {
525 assert_unchecked(!self.as_string_no_assert().is_empty());
526 }
527 }
528
529 const fn as_string_no_assert(&self) -> &String {
530 &self.inner
531 }
532
533 const unsafe fn as_mut_string_no_assert(&mut self) -> &mut String {
534 &mut self.inner
535 }
536
537 fn into_string_no_assert(self) -> String {
538 self.inner
539 }
540
541 #[must_use]
555 pub fn from_non_empty_str(string: &NonEmptyStr) -> Self {
556 unsafe { Self::new_unchecked(string.as_str().to_owned()) }
558 }
559
560 #[deprecated = "this string is never empty"]
564 #[must_use]
565 pub const fn is_empty(&self) -> bool {
566 false
567 }
568
569 #[must_use]
571 pub const fn len(&self) -> Size {
572 let len = self.as_string().len();
573
574 unsafe { Size::new_unchecked(len) }
576 }
577
578 #[must_use]
580 pub const fn capacity(&self) -> Size {
581 let capacity = self.as_string().capacity();
582
583 unsafe { Size::new_unchecked(capacity) }
585 }
586
587 #[must_use]
589 pub const fn as_str(&self) -> &str {
590 self.as_string().as_str()
591 }
592
593 pub const fn as_mut_str(&mut self) -> &mut str {
595 unsafe { self.as_mut_string().as_mut_str() }
597 }
598
599 #[must_use]
601 pub const fn as_non_empty_str(&self) -> &NonEmptyStr {
602 unsafe { NonEmptyStr::from_str_unchecked(self.as_str()) }
604 }
605
606 pub const fn as_non_empty_mut_str(&mut self) -> &mut NonEmptyStr {
608 unsafe { NonEmptyStr::from_mut_str_unchecked(self.as_mut_str()) }
610 }
611
612 #[must_use]
614 pub const fn as_bytes(&self) -> &Bytes {
615 self.as_str().as_bytes()
616 }
617
618 pub const unsafe fn as_bytes_mut(&mut self) -> &mut Bytes {
624 unsafe { self.as_mut_str().as_bytes_mut() }
627 }
628
629 #[must_use]
631 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
632 self.as_non_empty_str().as_non_empty_bytes()
633 }
634
635 pub const unsafe fn as_non_empty_bytes_mut(&mut self) -> &mut NonEmptyBytes {
641 unsafe { self.as_non_empty_mut_str().as_non_empty_bytes_mut() }
643 }
644
645 #[must_use]
647 pub const fn as_string(&self) -> &String {
648 #[cfg(feature = "unsafe-assert")]
649 self.assert_non_empty();
650
651 self.as_string_no_assert()
652 }
653
654 pub fn from_utf8_lossy(bytes: &Bytes) -> Result<NonEmptyCowStr<'_>, EmptySlice> {
662 NonEmptyBytes::try_from_slice(bytes).map(Self::from_non_empty_utf8_lossy)
663 }
664
665 pub fn from_utf8_lossy_owned(bytes: ByteVec) -> Result<Self, EmptyByteVec> {
674 NonEmptyByteVec::new(bytes).map(Self::from_non_empty_utf8_lossy_owned)
675 }
676
677 #[must_use]
684 pub fn from_non_empty_utf8_lossy(non_empty: &NonEmptyBytes) -> NonEmptyCowStr<'_> {
685 match String::from_utf8_lossy(non_empty.as_slice()) {
686 Cow::Owned(string) => Cow::Owned(unsafe { Self::new_unchecked(string) }),
688 Cow::Borrowed(string) => {
689 Cow::Borrowed(unsafe { NonEmptyStr::from_str_unchecked(string) })
691 }
692 }
693 }
694
695 #[must_use]
701 pub fn from_non_empty_utf8_lossy_owned(non_empty: NonEmptyByteVec) -> Self {
702 let cow = Self::from_non_empty_utf8_lossy(non_empty.as_non_empty_slice());
703
704 if let Cow::Owned(string) = cow {
705 string
706 } else {
707 unsafe { Self::from_non_empty_utf8_unchecked(non_empty) }
710 }
711 }
712
713 pub fn from_utf8(bytes: ByteVec) -> Result<Self, FromMaybeEmptyUtf8Error> {
719 let non_empty = NonEmptyByteVec::new(bytes)?;
720
721 let string = Self::from_non_empty_utf8(non_empty)?;
722
723 Ok(string)
724 }
725
726 pub fn from_non_empty_utf8(non_empty: NonEmptyByteVec) -> Result<Self, FromNonEmptyUtf8Error> {
732 let string = String::from_utf8(non_empty.into_vec()).map_err(|error| {
733 let non_empty_error = error.utf8_error().into();
734
735 let non_empty = unsafe { NonEmptyByteVec::new_unchecked(error.into_bytes()) };
737
738 FromNonEmptyUtf8Error::new(non_empty_error, non_empty)
739 })?;
740
741 Ok(unsafe { Self::new_unchecked(string) })
743 }
744
745 #[must_use]
751 pub unsafe fn from_non_empty_utf8_unchecked(non_empty: NonEmptyByteVec) -> Self {
752 unsafe { Self::from_utf8_unchecked(non_empty.into_vec()) }
755 }
756
757 #[must_use]
764 pub unsafe fn from_utf8_unchecked(bytes: ByteVec) -> Self {
765 unsafe { Self::new_unchecked(String::from_utf8_unchecked(bytes)) }
767 }
768
769 #[must_use]
775 pub const unsafe fn as_mut_string(&mut self) -> &mut String {
776 #[cfg(feature = "unsafe-assert")]
777 self.assert_non_empty();
778
779 unsafe { self.as_mut_string_no_assert() }
781 }
782
783 #[must_use]
785 pub fn into_string(self) -> String {
786 #[cfg(feature = "unsafe-assert")]
787 self.assert_non_empty();
788
789 self.into_string_no_assert()
790 }
791
792 #[must_use]
794 pub fn into_bytes(self) -> ByteVec {
795 self.into_string().into_bytes()
796 }
797
798 #[must_use]
800 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
801 unsafe { NonEmptyByteVec::new_unchecked(self.into_bytes()) }
803 }
804
805 pub fn push(&mut self, character: char) {
807 unsafe {
809 self.as_mut_string().push(character);
810 }
811 }
812
813 pub fn push_str(&mut self, string: &str) {
815 unsafe {
817 self.as_mut_string().push_str(string);
818 }
819 }
820
821 pub fn extend_from_within<R: RangeBounds<usize>>(&mut self, source: R) {
827 unsafe {
829 self.as_mut_string().extend_from_within(source);
830 }
831 }
832
833 pub fn extend_from<S: AsRef<str>>(&mut self, string: S) {
835 self.push_str(string.as_ref());
836 }
837
838 pub fn reserve(&mut self, additional: Size) {
850 unsafe {
852 self.as_mut_string().reserve(additional.get());
853 }
854 }
855
856 pub fn reserve_exact(&mut self, additional: Size) {
871 unsafe {
873 self.as_mut_string().reserve_exact(additional.get());
874 }
875 }
876
877 pub fn try_reserve(&mut self, additional: Size) -> Result<(), TryReserveError> {
889 unsafe { self.as_mut_string().try_reserve(additional.get()) }
891 }
892
893 pub fn try_reserve_exact(&mut self, additional: Size) -> Result<(), TryReserveError> {
908 unsafe { self.as_mut_string().try_reserve_exact(additional.get()) }
910 }
911
912 pub fn shrink_to_fit(&mut self) {
914 unsafe {
916 self.as_mut_string().shrink_to_fit();
917 }
918 }
919
920 pub fn shrink_to(&mut self, capacity: Size) {
926 unsafe {
928 self.as_mut_string().shrink_to(capacity.get());
929 }
930 }
931
932 pub fn truncate(&mut self, new: Size) {
938 unsafe {
940 self.as_mut_string().truncate(new.get());
941 }
942 }
943
944 #[must_use]
946 pub fn next_empty(&self) -> bool {
947 let (character, _) = self.chars().consume();
948
949 self.len().get() - character.len_utf8() == 0
950 }
951
952 #[must_use]
956 pub fn next_non_empty(&self) -> bool {
957 !self.next_empty()
958 }
959
960 pub fn pop(&mut self) -> Option<char> {
963 self.next_non_empty()
964 .then(|| unsafe { self.as_mut_string().pop() })
966 .flatten()
967 }
968
969 #[must_use]
971 pub fn leak<'a>(self) -> &'a mut str {
972 self.into_string().leak()
973 }
974
975 #[must_use]
979 pub fn leak_non_empty<'a>(self) -> &'a mut NonEmptyStr {
980 unsafe { NonEmptyStr::from_mut_str_unchecked(self.leak()) }
982 }
983
984 pub fn insert(&mut self, index: usize, character: char) {
991 unsafe {
993 self.as_mut_string().insert(index, character);
994 }
995 }
996
997 pub fn insert_str(&mut self, index: usize, string: &str) {
1003 unsafe {
1005 self.as_mut_string().insert_str(index, string);
1006 }
1007 }
1008
1009 pub fn insert_from<S: AsRef<str>>(&mut self, index: usize, string: S) {
1016 self.insert_str(index, string.as_ref());
1017 }
1018
1019 pub fn remove(&mut self, index: usize) -> Option<char> {
1028 self.next_non_empty()
1029 .then(|| unsafe { self.as_mut_string().remove(index) })
1031 }
1032
1033 pub fn split_off(&mut self, at: Size) -> String {
1041 unsafe { self.as_mut_string().split_off(at.get()) }
1043 }
1044}
1045
1046impl ToOwned for NonEmptyStr {
1047 type Owned = NonEmptyString;
1048
1049 fn to_owned(&self) -> Self::Owned {
1050 self.to_non_empty_string()
1051 }
1052}
1053
1054impl NonEmptyStr {
1055 #[must_use]
1057 pub fn to_non_empty_string(&self) -> NonEmptyString {
1058 NonEmptyString::from_non_empty_str(self)
1059 }
1060
1061 #[must_use]
1063 pub fn to_non_empty_bytes(&self) -> NonEmptyByteVec {
1064 self.to_non_empty_string().into_non_empty_bytes()
1065 }
1066
1067 #[must_use]
1069 pub fn to_lowercase(&self) -> String {
1070 self.as_str().to_lowercase()
1071 }
1072
1073 #[must_use]
1075 pub fn to_uppercase(&self) -> String {
1076 self.as_str().to_uppercase()
1077 }
1078
1079 #[must_use]
1088 pub fn repeat(&self, count: Size) -> NonEmptyString {
1089 let non_empty = self.as_str().repeat(count.get());
1090
1091 unsafe { NonEmptyString::new_unchecked(non_empty) }
1093 }
1094
1095 #[must_use]
1097 pub fn to_non_empty_lowercase(&self) -> NonEmptyString {
1098 unsafe { NonEmptyString::new_unchecked(self.to_lowercase()) }
1100 }
1101
1102 #[must_use]
1104 pub fn to_non_empty_uppercase(&self) -> NonEmptyString {
1105 unsafe { NonEmptyString::new_unchecked(self.to_uppercase()) }
1107 }
1108}
1109
1110impl NonEmptyString {
1111 #[must_use]
1113 pub fn single(character: char) -> Self {
1114 unsafe { Self::new_unchecked(character.to_string()) }
1116 }
1117
1118 #[must_use]
1120 pub fn with_capacity_and_char(capacity: Size, character: char) -> Self {
1121 let mut string = String::with_capacity(capacity.get());
1122
1123 string.push(character);
1124
1125 unsafe { Self::new_unchecked(string) }
1127 }
1128}
1129
1130impl FromNonEmptyIterator<char> for NonEmptyString {
1131 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = char>>(iterable: I) -> Self {
1132 let (character, iterator) = iterable.into_non_empty_iter().consume();
1133
1134 let mut output = Self::single(character);
1135
1136 output.extend(iterator);
1137
1138 output
1139 }
1140}
1141
1142impl<'c> FromNonEmptyIterator<&'c char> for NonEmptyString {
1143 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'c char>>(iterable: I) -> Self {
1144 let (&character, iterator) = iterable.into_non_empty_iter().consume();
1145
1146 let mut output = Self::single(character);
1147
1148 output.extend(iterator);
1149
1150 output
1151 }
1152}
1153
1154impl<'s> FromNonEmptyIterator<&'s NonEmptyStr> for NonEmptyString {
1155 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'s NonEmptyStr>>(iterable: I) -> Self {
1156 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1157
1158 let mut output = Self::from_non_empty_str(non_empty);
1159
1160 output.extend(iterator);
1161
1162 output
1163 }
1164}
1165
1166impl FromNonEmptyIterator<Self> for NonEmptyString {
1167 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = Self>>(iterable: I) -> Self {
1168 let (mut output, iterator) = iterable.into_non_empty_iter().consume();
1169
1170 output.extend(iterator);
1171
1172 output
1173 }
1174}
1175
1176impl FromNonEmptyIterator<NonEmptyBoxedStr> for NonEmptyString {
1177 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyBoxedStr>>(iterable: I) -> Self {
1178 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1179
1180 let mut output = Self::from_non_empty_boxed_str(non_empty);
1181
1182 output.extend(iterator);
1183
1184 output
1185 }
1186}
1187
1188impl<'s> FromNonEmptyIterator<NonEmptyCowStr<'s>> for NonEmptyString {
1189 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyCowStr<'s>>>(
1190 iterable: I,
1191 ) -> Self {
1192 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1193
1194 let mut output = non_empty.into_owned();
1195
1196 output.extend(iterator);
1197
1198 output
1199 }
1200}