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};
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};
23
24use non_empty_iter::{FromNonEmptyIterator, IntoNonEmptyIterator, NonEmptyIterator};
25use non_empty_slice::{EmptyByteVec, EmptySlice, NonEmptyByteVec, NonEmptyBytes};
26use non_zero_size::Size;
27
28use thiserror::Error;
29
30use crate::{
31 boxed::{EmptyBoxedStr, NonEmptyBoxedStr},
32 cow::NonEmptyCowStr,
33 internal::{ByteVec, Bytes},
34 str::{EmptyStr, FromNonEmptyStr, NonEmptyStr, NonEmptyUtf8Error},
35};
36
37pub const EMPTY_STRING: &str = "the string is empty";
39
40#[derive(Debug, Error)]
42#[error("{EMPTY_STRING}")]
43#[cfg_attr(
44 feature = "diagnostics",
45 derive(miette::Diagnostic),
46 diagnostic(code(non_empty_str::string), help("make sure the string is non-empty"))
47)]
48pub struct EmptyString {
49 string: String,
50}
51
52impl EmptyString {
53 pub(crate) const fn new(string: String) -> Self {
55 Self { string }
56 }
57
58 #[must_use]
60 pub fn get(self) -> String {
61 self.string
62 }
63
64 #[must_use]
66 pub fn from_empty_boxed_str(empty: EmptyBoxedStr) -> Self {
67 Self::new(empty.get().into_string())
68 }
69
70 #[must_use]
72 pub fn into_empty_boxed_str(self) -> EmptyBoxedStr {
73 EmptyBoxedStr::from_empty_string(self)
74 }
75}
76
77#[derive(Debug, Error)]
79#[error("{error}")]
80#[cfg_attr(
81 feature = "diagnostics",
82 derive(miette::Diagnostic),
83 diagnostic(
84 code(non_empty_str::string::utf8),
85 help("make sure the bytes are valid UTF-8")
86 )
87)]
88pub struct FromNonEmptyUtf8Error {
89 #[source]
90 #[cfg_attr(feature = "diagnostics", diagnostic_source)]
91 error: NonEmptyUtf8Error,
92 bytes: NonEmptyByteVec,
93}
94
95impl FromNonEmptyUtf8Error {
96 pub(crate) const fn new(error: NonEmptyUtf8Error, bytes: NonEmptyByteVec) -> Self {
98 Self { error, bytes }
99 }
100
101 #[must_use]
103 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
104 self.bytes.as_non_empty_slice()
105 }
106
107 #[must_use]
109 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
110 self.bytes
111 }
112
113 #[must_use]
115 pub const fn non_empty_error(&self) -> NonEmptyUtf8Error {
116 self.error
117 }
118
119 #[must_use]
126 pub fn recover(self) -> (NonEmptyUtf8Error, NonEmptyByteVec) {
127 (self.non_empty_error(), self.into_non_empty_bytes())
128 }
129}
130
131#[derive(Debug, Error)]
133#[error(transparent)]
134#[cfg_attr(
135 feature = "diagnostics",
136 derive(miette::Diagnostic),
137 diagnostic(transparent)
138)]
139pub enum FromMaybeEmptyUtf8Error {
140 Empty(#[from] EmptyByteVec),
142 Utf8(#[from] FromNonEmptyUtf8Error),
144}
145
146#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
148#[repr(transparent)]
149pub struct NonEmptyString {
150 inner: String,
151}
152
153impl Clone for NonEmptyString {
154 fn clone(&self) -> Self {
155 unsafe { Self::new_unchecked(self.as_string().clone()) }
157 }
158
159 fn clone_from(&mut self, source: &Self) {
160 unsafe {
162 self.as_mut_string().clone_from(source.as_string());
163 }
164 }
165}
166
167impl fmt::Write for NonEmptyString {
168 fn write_str(&mut self, string: &str) -> fmt::Result {
169 unsafe { self.as_mut_string().write_str(string) }
171 }
172
173 fn write_char(&mut self, character: char) -> fmt::Result {
174 unsafe { self.as_mut_string().write_char(character) }
176 }
177
178 fn write_fmt(&mut self, arguments: fmt::Arguments<'_>) -> fmt::Result {
179 unsafe { self.as_mut_string().write_fmt(arguments) }
181 }
182}
183
184impl fmt::Display for NonEmptyString {
185 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
186 self.as_string().fmt(formatter)
187 }
188}
189
190impl FromNonEmptyStr for NonEmptyString {
191 type Error = Infallible;
192
193 fn from_non_empty_str(string: &NonEmptyStr) -> Result<Self, Self::Error> {
194 Ok(Self::from_non_empty_str(string))
195 }
196}
197
198impl Borrow<NonEmptyStr> for NonEmptyString {
199 fn borrow(&self) -> &NonEmptyStr {
200 self.as_non_empty_str()
201 }
202}
203
204impl BorrowMut<NonEmptyStr> for NonEmptyString {
205 fn borrow_mut(&mut self) -> &mut NonEmptyStr {
206 self.as_non_empty_mut_str()
207 }
208}
209
210impl Borrow<str> for NonEmptyString {
211 fn borrow(&self) -> &str {
212 self.as_str()
213 }
214}
215
216impl BorrowMut<str> for NonEmptyString {
217 fn borrow_mut(&mut self) -> &mut str {
218 self.as_mut_str()
219 }
220}
221
222impl TryFrom<String> for NonEmptyString {
223 type Error = EmptyString;
224
225 fn try_from(string: String) -> Result<Self, Self::Error> {
226 Self::new(string)
227 }
228}
229
230impl From<NonEmptyString> for String {
231 fn from(non_empty: NonEmptyString) -> Self {
232 non_empty.into_string()
233 }
234}
235
236impl From<&NonEmptyStr> for NonEmptyString {
237 fn from(non_empty: &NonEmptyStr) -> Self {
238 non_empty.to_non_empty_string()
239 }
240}
241
242impl From<&mut NonEmptyStr> for NonEmptyString {
243 fn from(non_empty: &mut NonEmptyStr) -> Self {
244 non_empty.to_non_empty_string()
245 }
246}
247
248impl TryFrom<&str> for NonEmptyString {
249 type Error = EmptyStr;
250
251 fn try_from(string: &str) -> Result<Self, Self::Error> {
252 let non_empty_string: &NonEmptyStr = string.try_into()?;
253
254 Ok(non_empty_string.into())
255 }
256}
257
258impl TryFrom<&mut str> for NonEmptyString {
259 type Error = EmptyStr;
260
261 fn try_from(string: &mut str) -> Result<Self, Self::Error> {
262 let non_empty_string: &mut NonEmptyStr = string.try_into()?;
263
264 Ok(non_empty_string.into())
265 }
266}
267
268impl From<char> for NonEmptyString {
269 fn from(character: char) -> Self {
270 Self::single(character)
271 }
272}
273
274impl AsRef<Self> for NonEmptyString {
275 fn as_ref(&self) -> &Self {
276 self
277 }
278}
279
280impl AsMut<Self> for NonEmptyString {
281 fn as_mut(&mut self) -> &mut Self {
282 self
283 }
284}
285
286impl AsRef<String> for NonEmptyString {
287 fn as_ref(&self) -> &String {
288 self.as_string()
289 }
290}
291
292impl AsRef<NonEmptyStr> for NonEmptyString {
293 fn as_ref(&self) -> &NonEmptyStr {
294 self.as_non_empty_str()
295 }
296}
297
298impl AsMut<NonEmptyStr> for NonEmptyString {
299 fn as_mut(&mut self) -> &mut NonEmptyStr {
300 self.as_non_empty_mut_str()
301 }
302}
303
304impl AsRef<str> for NonEmptyString {
305 fn as_ref(&self) -> &str {
306 self.as_str()
307 }
308}
309
310impl AsMut<str> for NonEmptyString {
311 fn as_mut(&mut self) -> &mut str {
312 self.as_mut_str()
313 }
314}
315
316impl AsRef<Bytes> for NonEmptyString {
317 fn as_ref(&self) -> &Bytes {
318 self.as_bytes()
319 }
320}
321
322impl Deref for NonEmptyString {
323 type Target = NonEmptyStr;
324
325 fn deref(&self) -> &Self::Target {
326 self.as_non_empty_str()
327 }
328}
329
330impl DerefMut for NonEmptyString {
331 fn deref_mut(&mut self) -> &mut Self::Target {
332 self.as_non_empty_mut_str()
333 }
334}
335
336impl Add<&str> for NonEmptyString {
337 type Output = Self;
338
339 fn add(mut self, string: &str) -> Self::Output {
340 self.push_str(string);
341
342 self
343 }
344}
345
346impl Add<&NonEmptyStr> for NonEmptyString {
347 type Output = Self;
348
349 fn add(mut self, non_empty: &NonEmptyStr) -> Self::Output {
350 self.extend_from(non_empty);
351
352 self
353 }
354}
355
356impl AddAssign<&str> for NonEmptyString {
357 fn add_assign(&mut self, string: &str) {
358 self.push_str(string);
359 }
360}
361
362impl AddAssign<&NonEmptyStr> for NonEmptyString {
363 fn add_assign(&mut self, non_empty: &NonEmptyStr) {
364 self.extend_from(non_empty);
365 }
366}
367
368impl<'s> Extend<&'s str> for NonEmptyString {
369 fn extend<I: IntoIterator<Item = &'s str>>(&mut self, iterable: I) {
370 unsafe {
372 self.as_mut_string().extend(iterable);
373 }
374 }
375}
376
377impl<'s> Extend<&'s NonEmptyStr> for NonEmptyString {
378 fn extend<I: IntoIterator<Item = &'s NonEmptyStr>>(&mut self, iterable: I) {
379 self.extend(iterable.into_iter().map(NonEmptyStr::as_str));
380 }
381}
382
383impl<'c> Extend<&'c char> for NonEmptyString {
384 fn extend<I: IntoIterator<Item = &'c char>>(&mut self, iterable: I) {
385 unsafe {
387 self.as_mut_string().extend(iterable);
388 }
389 }
390}
391
392impl Extend<Box<str>> for NonEmptyString {
393 fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iterable: I) {
394 unsafe {
396 self.as_mut_string().extend(iterable);
397 }
398 }
399}
400
401impl Extend<NonEmptyBoxedStr> for NonEmptyString {
402 fn extend<I: IntoIterator<Item = NonEmptyBoxedStr>>(&mut self, iterable: I) {
403 self.extend(iterable.into_iter().map(NonEmptyStr::into_boxed_str));
404 }
405}
406
407impl<'s> Extend<Cow<'s, str>> for NonEmptyString {
408 fn extend<I: IntoIterator<Item = Cow<'s, str>>>(&mut self, iterable: I) {
409 unsafe {
411 self.as_mut_string().extend(iterable);
412 }
413 }
414}
415
416impl<'s> Extend<NonEmptyCowStr<'s>> for NonEmptyString {
417 fn extend<I: IntoIterator<Item = Cow<'s, NonEmptyStr>>>(&mut self, iterable: I) {
418 self.extend(iterable.into_iter().map(|non_empty| match non_empty {
419 Cow::Borrowed(string) => Cow::Borrowed(string.as_str()),
420 Cow::Owned(string) => Cow::Owned(string.into_string()),
421 }));
422 }
423}
424
425impl Extend<String> for NonEmptyString {
426 fn extend<I: IntoIterator<Item = String>>(&mut self, iterable: I) {
427 unsafe {
429 self.as_mut_string().extend(iterable);
430 }
431 }
432}
433
434impl Extend<Self> for NonEmptyString {
435 fn extend<I: IntoIterator<Item = Self>>(&mut self, iterable: I) {
436 self.extend(iterable.into_iter().map(Self::into_string));
437 }
438}
439
440impl Extend<char> for NonEmptyString {
441 fn extend<I: IntoIterator<Item = char>>(&mut self, iterable: I) {
442 unsafe {
444 self.as_mut_string().extend(iterable);
445 }
446 }
447}
448
449impl NonEmptyString {
450 pub const fn new(string: String) -> Result<Self, EmptyString> {
476 if string.is_empty() {
477 return Err(EmptyString::new(string));
478 }
479
480 Ok(unsafe { Self::new_unchecked(string) })
482 }
483
484 #[must_use]
490 pub const unsafe fn new_unchecked(inner: String) -> Self {
491 debug_assert!(!inner.is_empty());
492
493 Self { inner }
494 }
495
496 #[cfg(feature = "unsafe-assert")]
497 const fn assert_non_empty(&self) {
498 use core::hint::assert_unchecked;
499
500 unsafe {
502 assert_unchecked(!self.as_string_no_assert().is_empty());
503 }
504 }
505
506 const fn as_string_no_assert(&self) -> &String {
507 &self.inner
508 }
509
510 const unsafe fn as_mut_string_no_assert(&mut self) -> &mut String {
511 &mut self.inner
512 }
513
514 fn into_string_no_assert(self) -> String {
515 self.inner
516 }
517
518 #[must_use]
532 pub fn from_non_empty_str(string: &NonEmptyStr) -> Self {
533 unsafe { Self::new_unchecked(string.as_str().to_owned()) }
535 }
536
537 #[deprecated = "this string is never empty"]
541 #[must_use]
542 pub const fn is_empty(&self) -> bool {
543 false
544 }
545
546 #[must_use]
548 pub const fn len(&self) -> Size {
549 let len = self.as_string().len();
550
551 unsafe { Size::new_unchecked(len) }
553 }
554
555 #[must_use]
557 pub const fn capacity(&self) -> Size {
558 let capacity = self.as_string().capacity();
559
560 unsafe { Size::new_unchecked(capacity) }
562 }
563
564 #[must_use]
566 pub const fn as_str(&self) -> &str {
567 self.as_string().as_str()
568 }
569
570 pub const fn as_mut_str(&mut self) -> &mut str {
572 unsafe { self.as_mut_string().as_mut_str() }
574 }
575
576 #[must_use]
578 pub const fn as_non_empty_str(&self) -> &NonEmptyStr {
579 unsafe { NonEmptyStr::from_str_unchecked(self.as_str()) }
581 }
582
583 pub const fn as_non_empty_mut_str(&mut self) -> &mut NonEmptyStr {
585 unsafe { NonEmptyStr::from_mut_str_unchecked(self.as_mut_str()) }
587 }
588
589 #[must_use]
591 pub const fn as_bytes(&self) -> &Bytes {
592 self.as_str().as_bytes()
593 }
594
595 pub const unsafe fn as_bytes_mut(&mut self) -> &mut Bytes {
601 unsafe { self.as_mut_str().as_bytes_mut() }
604 }
605
606 #[must_use]
608 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
609 self.as_non_empty_str().as_non_empty_bytes()
610 }
611
612 pub const unsafe fn as_non_empty_bytes_mut(&mut self) -> &mut NonEmptyBytes {
618 unsafe { self.as_non_empty_mut_str().as_non_empty_bytes_mut() }
620 }
621
622 #[must_use]
624 pub const fn as_string(&self) -> &String {
625 #[cfg(feature = "unsafe-assert")]
626 self.assert_non_empty();
627
628 self.as_string_no_assert()
629 }
630
631 pub fn from_utf8_lossy(bytes: &Bytes) -> Result<NonEmptyCowStr<'_>, EmptySlice> {
639 NonEmptyBytes::try_from_slice(bytes).map(Self::from_non_empty_utf8_lossy)
640 }
641
642 pub fn from_utf8_lossy_owned(bytes: ByteVec) -> Result<Self, EmptyByteVec> {
651 NonEmptyByteVec::new(bytes).map(Self::from_non_empty_utf8_lossy_owned)
652 }
653
654 #[must_use]
661 pub fn from_non_empty_utf8_lossy(non_empty: &NonEmptyBytes) -> NonEmptyCowStr<'_> {
662 match String::from_utf8_lossy(non_empty.as_slice()) {
663 Cow::Owned(string) => Cow::Owned(unsafe { Self::new_unchecked(string) }),
665 Cow::Borrowed(string) => {
666 Cow::Borrowed(unsafe { NonEmptyStr::from_str_unchecked(string) })
668 }
669 }
670 }
671
672 #[must_use]
678 pub fn from_non_empty_utf8_lossy_owned(non_empty: NonEmptyByteVec) -> Self {
679 let cow = Self::from_non_empty_utf8_lossy(non_empty.as_non_empty_slice());
680
681 if let Cow::Owned(string) = cow {
682 string
683 } else {
684 unsafe { Self::from_non_empty_utf8_unchecked(non_empty) }
687 }
688 }
689
690 pub fn from_utf8(bytes: ByteVec) -> Result<Self, FromMaybeEmptyUtf8Error> {
696 let non_empty = NonEmptyByteVec::new(bytes)?;
697
698 let string = Self::from_non_empty_utf8(non_empty)?;
699
700 Ok(string)
701 }
702
703 pub fn from_non_empty_utf8(non_empty: NonEmptyByteVec) -> Result<Self, FromNonEmptyUtf8Error> {
709 let string = String::from_utf8(non_empty.into_vec()).map_err(|error| {
710 let non_empty_error = error.utf8_error().into();
711
712 let non_empty = unsafe { NonEmptyByteVec::new_unchecked(error.into_bytes()) };
714
715 FromNonEmptyUtf8Error::new(non_empty_error, non_empty)
716 })?;
717
718 Ok(unsafe { Self::new_unchecked(string) })
720 }
721
722 #[must_use]
728 pub unsafe fn from_non_empty_utf8_unchecked(non_empty: NonEmptyByteVec) -> Self {
729 unsafe { Self::from_utf8_unchecked(non_empty.into_vec()) }
732 }
733
734 #[must_use]
741 pub unsafe fn from_utf8_unchecked(bytes: ByteVec) -> Self {
742 unsafe { Self::new_unchecked(String::from_utf8_unchecked(bytes)) }
744 }
745
746 #[must_use]
752 pub const unsafe fn as_mut_string(&mut self) -> &mut String {
753 #[cfg(feature = "unsafe-assert")]
754 self.assert_non_empty();
755
756 unsafe { self.as_mut_string_no_assert() }
758 }
759
760 #[must_use]
762 pub fn into_string(self) -> String {
763 #[cfg(feature = "unsafe-assert")]
764 self.assert_non_empty();
765
766 self.into_string_no_assert()
767 }
768
769 #[must_use]
771 pub fn into_bytes(self) -> ByteVec {
772 self.into_string().into_bytes()
773 }
774
775 #[must_use]
777 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
778 unsafe { NonEmptyByteVec::new_unchecked(self.into_bytes()) }
780 }
781
782 pub fn push(&mut self, character: char) {
784 unsafe {
786 self.as_mut_string().push(character);
787 }
788 }
789
790 pub fn push_str(&mut self, string: &str) {
792 unsafe {
794 self.as_mut_string().push_str(string);
795 }
796 }
797
798 pub fn extend_from_within<R: RangeBounds<usize>>(&mut self, source: R) {
804 unsafe {
806 self.as_mut_string().extend_from_within(source);
807 }
808 }
809
810 pub fn extend_from<S: AsRef<str>>(&mut self, string: S) {
812 self.push_str(string.as_ref());
813 }
814
815 pub fn reserve(&mut self, additional: Size) {
827 unsafe {
829 self.as_mut_string().reserve(additional.get());
830 }
831 }
832
833 pub fn reserve_exact(&mut self, additional: Size) {
848 unsafe {
850 self.as_mut_string().reserve_exact(additional.get());
851 }
852 }
853
854 pub fn try_reserve(&mut self, additional: Size) -> Result<(), TryReserveError> {
866 unsafe { self.as_mut_string().try_reserve(additional.get()) }
868 }
869
870 pub fn try_reserve_exact(&mut self, additional: Size) -> Result<(), TryReserveError> {
885 unsafe { self.as_mut_string().try_reserve_exact(additional.get()) }
887 }
888
889 pub fn shrink_to_fit(&mut self) {
891 unsafe {
893 self.as_mut_string().shrink_to_fit();
894 }
895 }
896
897 pub fn shrink_to(&mut self, capacity: Size) {
903 unsafe {
905 self.as_mut_string().shrink_to(capacity.get());
906 }
907 }
908
909 pub fn truncate(&mut self, new: Size) {
915 unsafe {
917 self.as_mut_string().truncate(new.get());
918 }
919 }
920
921 #[must_use]
923 pub fn next_empty(&self) -> bool {
924 let (character, _) = self.chars().consume();
925
926 self.len().get() - character.len_utf8() == 0
927 }
928
929 #[must_use]
933 pub fn next_non_empty(&self) -> bool {
934 !self.next_empty()
935 }
936
937 pub fn pop(&mut self) -> Option<char> {
940 self.next_non_empty()
941 .then(|| unsafe { self.as_mut_string().pop() })
943 .flatten()
944 }
945
946 #[must_use]
948 pub fn leak<'a>(self) -> &'a mut str {
949 self.into_string().leak()
950 }
951
952 #[must_use]
956 pub fn leak_non_empty<'a>(self) -> &'a mut NonEmptyStr {
957 unsafe { NonEmptyStr::from_mut_str_unchecked(self.leak()) }
959 }
960
961 pub fn insert(&mut self, index: usize, character: char) {
968 unsafe {
970 self.as_mut_string().insert(index, character);
971 }
972 }
973
974 pub fn insert_str(&mut self, index: usize, string: &str) {
980 unsafe {
982 self.as_mut_string().insert_str(index, string);
983 }
984 }
985
986 pub fn insert_from<S: AsRef<str>>(&mut self, index: usize, string: S) {
993 self.insert_str(index, string.as_ref());
994 }
995
996 pub fn remove(&mut self, index: usize) -> Option<char> {
1005 self.next_non_empty()
1006 .then(|| unsafe { self.as_mut_string().remove(index) })
1008 }
1009
1010 pub fn split_off(&mut self, at: Size) -> String {
1018 unsafe { self.as_mut_string().split_off(at.get()) }
1020 }
1021}
1022
1023impl ToOwned for NonEmptyStr {
1024 type Owned = NonEmptyString;
1025
1026 fn to_owned(&self) -> Self::Owned {
1027 self.to_non_empty_string()
1028 }
1029}
1030
1031impl NonEmptyStr {
1032 #[must_use]
1034 pub fn to_non_empty_string(&self) -> NonEmptyString {
1035 NonEmptyString::from_non_empty_str(self)
1036 }
1037
1038 #[must_use]
1040 pub fn to_non_empty_bytes(&self) -> NonEmptyByteVec {
1041 self.to_non_empty_string().into_non_empty_bytes()
1042 }
1043
1044 #[must_use]
1046 pub fn to_lowercase(&self) -> String {
1047 self.as_str().to_lowercase()
1048 }
1049
1050 #[must_use]
1052 pub fn to_uppercase(&self) -> String {
1053 self.as_str().to_uppercase()
1054 }
1055
1056 #[must_use]
1065 pub fn repeat(&self, count: Size) -> NonEmptyString {
1066 let non_empty = self.as_str().repeat(count.get());
1067
1068 unsafe { NonEmptyString::new_unchecked(non_empty) }
1070 }
1071
1072 #[must_use]
1074 pub fn to_non_empty_lowercase(&self) -> NonEmptyString {
1075 unsafe { NonEmptyString::new_unchecked(self.to_lowercase()) }
1077 }
1078
1079 #[must_use]
1081 pub fn to_non_empty_uppercase(&self) -> NonEmptyString {
1082 unsafe { NonEmptyString::new_unchecked(self.to_uppercase()) }
1084 }
1085}
1086
1087impl NonEmptyString {
1088 #[must_use]
1090 pub fn single(character: char) -> Self {
1091 unsafe { Self::new_unchecked(character.to_string()) }
1093 }
1094
1095 #[must_use]
1097 pub fn with_capacity_and_char(capacity: Size, character: char) -> Self {
1098 let mut string = String::with_capacity(capacity.get());
1099
1100 string.push(character);
1101
1102 unsafe { Self::new_unchecked(string) }
1104 }
1105}
1106
1107impl FromNonEmptyIterator<char> for NonEmptyString {
1108 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = char>>(iterable: I) -> Self {
1109 let (character, iterator) = iterable.into_non_empty_iter().consume();
1110
1111 let mut output = Self::single(character);
1112
1113 output.extend(iterator);
1114
1115 output
1116 }
1117}
1118
1119impl<'c> FromNonEmptyIterator<&'c char> for NonEmptyString {
1120 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'c char>>(iterable: I) -> Self {
1121 let (&character, iterator) = iterable.into_non_empty_iter().consume();
1122
1123 let mut output = Self::single(character);
1124
1125 output.extend(iterator);
1126
1127 output
1128 }
1129}
1130
1131impl<'s> FromNonEmptyIterator<&'s NonEmptyStr> for NonEmptyString {
1132 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'s NonEmptyStr>>(iterable: I) -> Self {
1133 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1134
1135 let mut output = Self::from_non_empty_str(non_empty);
1136
1137 output.extend(iterator);
1138
1139 output
1140 }
1141}
1142
1143impl FromNonEmptyIterator<Self> for NonEmptyString {
1144 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = Self>>(iterable: I) -> Self {
1145 let (mut output, iterator) = iterable.into_non_empty_iter().consume();
1146
1147 output.extend(iterator);
1148
1149 output
1150 }
1151}
1152
1153impl FromNonEmptyIterator<NonEmptyBoxedStr> for NonEmptyString {
1154 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyBoxedStr>>(iterable: I) -> Self {
1155 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1156
1157 let mut output = Self::from_non_empty_boxed_str(non_empty);
1158
1159 output.extend(iterator);
1160
1161 output
1162 }
1163}
1164
1165impl<'s> FromNonEmptyIterator<NonEmptyCowStr<'s>> for NonEmptyString {
1166 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyCowStr<'s>>>(
1167 iterable: I,
1168 ) -> Self {
1169 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1170
1171 let mut output = non_empty.into_owned();
1172
1173 output.extend(iterator);
1174
1175 output
1176 }
1177}