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 hash::{Hash, Hasher},
22 ops::{Add, AddAssign, Deref, DerefMut, RangeBounds},
23 str::FromStr,
24};
25
26use non_empty_iter::{FromNonEmptyIterator, IntoNonEmptyIterator, NonEmptyIterator};
27use non_empty_slice::{EmptyByteVec, EmptySlice, NonEmptyByteVec, NonEmptyBytes};
28use non_zero_size::Size;
29
30use thiserror::Error;
31
32use crate::{
33 boxed::{EmptyBoxedStr, NonEmptyBoxedStr},
34 cow::NonEmptyCowStr,
35 internals::{ByteVec, Bytes},
36 str::{EmptyStr, FromNonEmptyStr, NonEmptyStr, NonEmptyUtf8Error},
37};
38
39pub const EMPTY_STRING: &str = "the string is empty";
41
42#[derive(Debug, Error)]
44#[error("{EMPTY_STRING}")]
45pub struct EmptyString {
46 string: String,
47}
48
49impl EmptyString {
50 pub(crate) const fn new(string: String) -> Self {
52 Self { string }
53 }
54
55 #[must_use]
57 pub fn get(self) -> String {
58 self.string
59 }
60
61 #[must_use]
63 pub fn from_empty_boxed_str(empty: EmptyBoxedStr) -> Self {
64 Self::new(empty.get().into_string())
65 }
66
67 #[must_use]
69 pub fn into_empty_boxed_str(self) -> EmptyBoxedStr {
70 EmptyBoxedStr::from_empty_string(self)
71 }
72}
73
74#[derive(Debug, Error)]
76#[error("{error}")]
77pub struct FromNonEmptyUtf8Error {
78 #[source]
79 error: NonEmptyUtf8Error,
80 bytes: NonEmptyByteVec,
81}
82
83impl FromNonEmptyUtf8Error {
84 pub(crate) const fn new(error: NonEmptyUtf8Error, bytes: NonEmptyByteVec) -> Self {
86 Self { error, bytes }
87 }
88
89 #[must_use]
91 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
92 self.bytes.as_non_empty_slice()
93 }
94
95 #[must_use]
97 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
98 self.bytes
99 }
100
101 #[must_use]
103 pub const fn non_empty_error(&self) -> NonEmptyUtf8Error {
104 self.error
105 }
106
107 #[must_use]
114 pub fn recover(self) -> (NonEmptyUtf8Error, NonEmptyByteVec) {
115 (self.non_empty_error(), self.into_non_empty_bytes())
116 }
117}
118
119#[derive(Debug, Error)]
121#[error(transparent)]
122pub enum FromMaybeEmptyUtf8Error {
123 Empty(#[from] EmptyByteVec),
125 Utf8(#[from] FromNonEmptyUtf8Error),
127}
128
129#[derive(Debug)]
131#[repr(transparent)]
132pub struct NonEmptyString {
133 inner: String,
134}
135
136impl Hash for NonEmptyString {
137 fn hash<H: Hasher>(&self, state: &mut H) {
138 self.as_string().hash(state);
139 }
140}
141
142impl Clone for NonEmptyString {
143 fn clone(&self) -> Self {
144 unsafe { Self::new_unchecked(self.as_string().clone()) }
146 }
147
148 fn clone_from(&mut self, source: &Self) {
149 unsafe {
151 self.as_mut_string().clone_from(source.as_string());
152 }
153 }
154}
155
156impl fmt::Write for NonEmptyString {
157 fn write_str(&mut self, string: &str) -> fmt::Result {
158 unsafe { self.as_mut_string().write_str(string) }
160 }
161
162 fn write_char(&mut self, character: char) -> fmt::Result {
163 unsafe { self.as_mut_string().write_char(character) }
165 }
166
167 fn write_fmt(&mut self, arguments: fmt::Arguments<'_>) -> fmt::Result {
168 unsafe { self.as_mut_string().write_fmt(arguments) }
170 }
171}
172
173impl fmt::Display for NonEmptyString {
174 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
175 self.as_string().fmt(formatter)
176 }
177}
178
179impl FromStr for NonEmptyString {
180 type Err = EmptyStr;
181
182 fn from_str(string: &str) -> Result<Self, Self::Err> {
183 NonEmptyStr::try_from_str(string).map(Self::from_non_empty_str)
184 }
185}
186
187impl FromNonEmptyStr for NonEmptyString {
188 type Error = Infallible;
189
190 fn from_non_empty_str(string: &NonEmptyStr) -> Result<Self, Self::Error> {
191 Ok(Self::from_non_empty_str(string))
192 }
193}
194
195impl Borrow<NonEmptyStr> for NonEmptyString {
196 fn borrow(&self) -> &NonEmptyStr {
197 self.as_non_empty_str()
198 }
199}
200
201impl BorrowMut<NonEmptyStr> for NonEmptyString {
202 fn borrow_mut(&mut self) -> &mut NonEmptyStr {
203 self.as_non_empty_mut_str()
204 }
205}
206
207impl Borrow<str> for NonEmptyString {
208 fn borrow(&self) -> &str {
209 self.as_str()
210 }
211}
212
213impl BorrowMut<str> for NonEmptyString {
214 fn borrow_mut(&mut self) -> &mut str {
215 self.as_mut_str()
216 }
217}
218
219impl TryFrom<String> for NonEmptyString {
220 type Error = EmptyString;
221
222 fn try_from(string: String) -> Result<Self, Self::Error> {
223 Self::new(string)
224 }
225}
226
227impl From<NonEmptyString> for String {
228 fn from(non_empty: NonEmptyString) -> Self {
229 non_empty.into_string()
230 }
231}
232
233impl From<&NonEmptyStr> for NonEmptyString {
234 fn from(non_empty: &NonEmptyStr) -> Self {
235 non_empty.to_non_empty_string()
236 }
237}
238
239impl From<&mut NonEmptyStr> for NonEmptyString {
240 fn from(non_empty: &mut NonEmptyStr) -> Self {
241 non_empty.to_non_empty_string()
242 }
243}
244
245impl TryFrom<&str> for NonEmptyString {
246 type Error = EmptyStr;
247
248 fn try_from(string: &str) -> Result<Self, Self::Error> {
249 let non_empty_string: &NonEmptyStr = string.try_into()?;
250
251 Ok(non_empty_string.into())
252 }
253}
254
255impl TryFrom<&mut str> for NonEmptyString {
256 type Error = EmptyStr;
257
258 fn try_from(string: &mut str) -> Result<Self, Self::Error> {
259 let non_empty_string: &mut NonEmptyStr = string.try_into()?;
260
261 Ok(non_empty_string.into())
262 }
263}
264
265impl From<char> for NonEmptyString {
266 fn from(character: char) -> Self {
267 Self::single(character)
268 }
269}
270
271impl AsRef<Self> for NonEmptyString {
272 fn as_ref(&self) -> &Self {
273 self
274 }
275}
276
277impl AsMut<Self> for NonEmptyString {
278 fn as_mut(&mut self) -> &mut Self {
279 self
280 }
281}
282
283impl AsRef<String> for NonEmptyString {
284 fn as_ref(&self) -> &String {
285 self.as_string()
286 }
287}
288
289impl AsRef<NonEmptyStr> for NonEmptyString {
290 fn as_ref(&self) -> &NonEmptyStr {
291 self.as_non_empty_str()
292 }
293}
294
295impl AsMut<NonEmptyStr> for NonEmptyString {
296 fn as_mut(&mut self) -> &mut NonEmptyStr {
297 self.as_non_empty_mut_str()
298 }
299}
300
301impl AsRef<str> for NonEmptyString {
302 fn as_ref(&self) -> &str {
303 self.as_str()
304 }
305}
306
307impl AsMut<str> for NonEmptyString {
308 fn as_mut(&mut self) -> &mut str {
309 self.as_mut_str()
310 }
311}
312
313impl AsRef<Bytes> for NonEmptyString {
314 fn as_ref(&self) -> &Bytes {
315 self.as_bytes()
316 }
317}
318
319#[cfg(feature = "std")]
320impl AsRef<OsStr> for NonEmptyString {
321 fn as_ref(&self) -> &OsStr {
322 self.as_string().as_ref()
323 }
324}
325
326#[cfg(feature = "std")]
327impl AsRef<Path> for NonEmptyString {
328 fn as_ref(&self) -> &Path {
329 self.as_string().as_ref()
330 }
331}
332
333impl Deref for NonEmptyString {
334 type Target = NonEmptyStr;
335
336 fn deref(&self) -> &Self::Target {
337 self.as_non_empty_str()
338 }
339}
340
341impl DerefMut for NonEmptyString {
342 fn deref_mut(&mut self) -> &mut Self::Target {
343 self.as_non_empty_mut_str()
344 }
345}
346
347impl Add<&str> for NonEmptyString {
348 type Output = Self;
349
350 fn add(mut self, string: &str) -> Self::Output {
351 self.push_str(string);
352
353 self
354 }
355}
356
357impl Add<&NonEmptyStr> for NonEmptyString {
358 type Output = Self;
359
360 fn add(mut self, non_empty: &NonEmptyStr) -> Self::Output {
361 self.extend_from(non_empty);
362
363 self
364 }
365}
366
367impl AddAssign<&str> for NonEmptyString {
368 fn add_assign(&mut self, string: &str) {
369 self.push_str(string);
370 }
371}
372
373impl AddAssign<&NonEmptyStr> for NonEmptyString {
374 fn add_assign(&mut self, non_empty: &NonEmptyStr) {
375 self.extend_from(non_empty);
376 }
377}
378
379impl<'s> Extend<&'s str> for NonEmptyString {
380 fn extend<I: IntoIterator<Item = &'s str>>(&mut self, iterable: I) {
381 unsafe {
383 self.as_mut_string().extend(iterable);
384 }
385 }
386}
387
388impl<'s> Extend<&'s NonEmptyStr> for NonEmptyString {
389 fn extend<I: IntoIterator<Item = &'s NonEmptyStr>>(&mut self, iterable: I) {
390 self.extend(iterable.into_iter().map(NonEmptyStr::as_str));
391 }
392}
393
394impl<'c> Extend<&'c char> for NonEmptyString {
395 fn extend<I: IntoIterator<Item = &'c char>>(&mut self, iterable: I) {
396 unsafe {
398 self.as_mut_string().extend(iterable);
399 }
400 }
401}
402
403impl Extend<Box<str>> for NonEmptyString {
404 fn extend<I: IntoIterator<Item = Box<str>>>(&mut self, iterable: I) {
405 unsafe {
407 self.as_mut_string().extend(iterable);
408 }
409 }
410}
411
412impl Extend<NonEmptyBoxedStr> for NonEmptyString {
413 fn extend<I: IntoIterator<Item = NonEmptyBoxedStr>>(&mut self, iterable: I) {
414 self.extend(iterable.into_iter().map(NonEmptyStr::into_boxed_str));
415 }
416}
417
418impl<'s> Extend<Cow<'s, str>> for NonEmptyString {
419 fn extend<I: IntoIterator<Item = Cow<'s, str>>>(&mut self, iterable: I) {
420 unsafe {
422 self.as_mut_string().extend(iterable);
423 }
424 }
425}
426
427impl<'s> Extend<NonEmptyCowStr<'s>> for NonEmptyString {
428 fn extend<I: IntoIterator<Item = Cow<'s, NonEmptyStr>>>(&mut self, iterable: I) {
429 self.extend(iterable.into_iter().map(|non_empty| match non_empty {
430 Cow::Borrowed(string) => Cow::Borrowed(string.as_str()),
431 Cow::Owned(string) => Cow::Owned(string.into_string()),
432 }));
433 }
434}
435
436impl Extend<String> for NonEmptyString {
437 fn extend<I: IntoIterator<Item = String>>(&mut self, iterable: I) {
438 unsafe {
440 self.as_mut_string().extend(iterable);
441 }
442 }
443}
444
445impl Extend<Self> for NonEmptyString {
446 fn extend<I: IntoIterator<Item = Self>>(&mut self, iterable: I) {
447 self.extend(iterable.into_iter().map(Self::into_string));
448 }
449}
450
451impl Extend<char> for NonEmptyString {
452 fn extend<I: IntoIterator<Item = char>>(&mut self, iterable: I) {
453 unsafe {
455 self.as_mut_string().extend(iterable);
456 }
457 }
458}
459
460impl NonEmptyString {
461 pub const fn new(string: String) -> Result<Self, EmptyString> {
487 if string.is_empty() {
488 return Err(EmptyString::new(string));
489 }
490
491 Ok(unsafe { Self::new_unchecked(string) })
493 }
494
495 #[must_use]
501 pub const unsafe fn new_unchecked(inner: String) -> Self {
502 debug_assert!(!inner.is_empty());
503
504 Self { inner }
505 }
506
507 #[cfg(feature = "unsafe-assert")]
508 const fn assert_non_empty(&self) {
509 use core::hint::assert_unchecked;
510
511 unsafe {
513 assert_unchecked(!self.as_string_no_assert().is_empty());
514 }
515 }
516
517 const fn as_string_no_assert(&self) -> &String {
518 &self.inner
519 }
520
521 const unsafe fn as_mut_string_no_assert(&mut self) -> &mut String {
522 &mut self.inner
523 }
524
525 fn into_string_no_assert(self) -> String {
526 self.inner
527 }
528
529 #[must_use]
543 pub fn from_non_empty_str(string: &NonEmptyStr) -> Self {
544 unsafe { Self::new_unchecked(string.as_str().to_owned()) }
546 }
547
548 #[deprecated = "this string is never empty"]
552 #[must_use]
553 pub const fn is_empty(&self) -> bool {
554 false
555 }
556
557 #[must_use]
559 pub const fn len(&self) -> Size {
560 let len = self.as_string().len();
561
562 unsafe { Size::new_unchecked(len) }
564 }
565
566 #[must_use]
568 pub const fn capacity(&self) -> Size {
569 let capacity = self.as_string().capacity();
570
571 unsafe { Size::new_unchecked(capacity) }
573 }
574
575 #[must_use]
577 pub const fn as_str(&self) -> &str {
578 self.as_string().as_str()
579 }
580
581 pub const fn as_mut_str(&mut self) -> &mut str {
583 unsafe { self.as_mut_string().as_mut_str() }
585 }
586
587 #[must_use]
589 pub const fn as_non_empty_str(&self) -> &NonEmptyStr {
590 unsafe { NonEmptyStr::from_str_unchecked(self.as_str()) }
592 }
593
594 pub const fn as_non_empty_mut_str(&mut self) -> &mut NonEmptyStr {
596 unsafe { NonEmptyStr::from_mut_str_unchecked(self.as_mut_str()) }
598 }
599
600 #[must_use]
602 pub const fn as_bytes(&self) -> &Bytes {
603 self.as_str().as_bytes()
604 }
605
606 pub const unsafe fn as_bytes_mut(&mut self) -> &mut Bytes {
612 unsafe { self.as_mut_str().as_bytes_mut() }
615 }
616
617 #[must_use]
619 pub const fn as_non_empty_bytes(&self) -> &NonEmptyBytes {
620 self.as_non_empty_str().as_non_empty_bytes()
621 }
622
623 pub const unsafe fn as_non_empty_bytes_mut(&mut self) -> &mut NonEmptyBytes {
629 unsafe { self.as_non_empty_mut_str().as_non_empty_bytes_mut() }
631 }
632
633 #[must_use]
635 pub const fn as_string(&self) -> &String {
636 #[cfg(feature = "unsafe-assert")]
637 self.assert_non_empty();
638
639 self.as_string_no_assert()
640 }
641
642 pub fn from_utf8_lossy(bytes: &Bytes) -> Result<NonEmptyCowStr<'_>, EmptySlice> {
650 NonEmptyBytes::try_from_slice(bytes).map(Self::from_non_empty_utf8_lossy)
651 }
652
653 pub fn from_utf8_lossy_owned(bytes: ByteVec) -> Result<Self, EmptyByteVec> {
662 NonEmptyByteVec::new(bytes).map(Self::from_non_empty_utf8_lossy_owned)
663 }
664
665 #[must_use]
672 pub fn from_non_empty_utf8_lossy(non_empty: &NonEmptyBytes) -> NonEmptyCowStr<'_> {
673 match String::from_utf8_lossy(non_empty.as_slice()) {
674 Cow::Owned(string) => Cow::Owned(unsafe { Self::new_unchecked(string) }),
676 Cow::Borrowed(string) => {
677 Cow::Borrowed(unsafe { NonEmptyStr::from_str_unchecked(string) })
679 }
680 }
681 }
682
683 #[must_use]
689 pub fn from_non_empty_utf8_lossy_owned(non_empty: NonEmptyByteVec) -> Self {
690 let cow = Self::from_non_empty_utf8_lossy(non_empty.as_non_empty_slice());
691
692 if let Cow::Owned(string) = cow {
693 string
694 } else {
695 unsafe { Self::from_non_empty_utf8_unchecked(non_empty) }
698 }
699 }
700
701 pub fn from_utf8(bytes: ByteVec) -> Result<Self, FromMaybeEmptyUtf8Error> {
707 let non_empty = NonEmptyByteVec::new(bytes)?;
708
709 let string = Self::from_non_empty_utf8(non_empty)?;
710
711 Ok(string)
712 }
713
714 pub fn from_non_empty_utf8(non_empty: NonEmptyByteVec) -> Result<Self, FromNonEmptyUtf8Error> {
720 let string = String::from_utf8(non_empty.into_vec()).map_err(|error| {
721 let non_empty_error = error.utf8_error().into();
722
723 let non_empty = unsafe { NonEmptyByteVec::new_unchecked(error.into_bytes()) };
725
726 FromNonEmptyUtf8Error::new(non_empty_error, non_empty)
727 })?;
728
729 Ok(unsafe { Self::new_unchecked(string) })
731 }
732
733 #[must_use]
739 pub unsafe fn from_non_empty_utf8_unchecked(non_empty: NonEmptyByteVec) -> Self {
740 unsafe { Self::from_utf8_unchecked(non_empty.into_vec()) }
743 }
744
745 #[must_use]
752 pub unsafe fn from_utf8_unchecked(bytes: ByteVec) -> Self {
753 unsafe { Self::new_unchecked(String::from_utf8_unchecked(bytes)) }
755 }
756
757 #[must_use]
763 pub const unsafe fn as_mut_string(&mut self) -> &mut String {
764 #[cfg(feature = "unsafe-assert")]
765 self.assert_non_empty();
766
767 unsafe { self.as_mut_string_no_assert() }
769 }
770
771 #[must_use]
773 pub fn into_string(self) -> String {
774 #[cfg(feature = "unsafe-assert")]
775 self.assert_non_empty();
776
777 self.into_string_no_assert()
778 }
779
780 #[must_use]
782 pub fn into_bytes(self) -> ByteVec {
783 self.into_string().into_bytes()
784 }
785
786 #[must_use]
788 pub fn into_non_empty_bytes(self) -> NonEmptyByteVec {
789 unsafe { NonEmptyByteVec::new_unchecked(self.into_bytes()) }
791 }
792
793 pub fn push(&mut self, character: char) {
795 unsafe {
797 self.as_mut_string().push(character);
798 }
799 }
800
801 pub fn push_str(&mut self, string: &str) {
803 unsafe {
805 self.as_mut_string().push_str(string);
806 }
807 }
808
809 pub fn extend_from_within<R: RangeBounds<usize>>(&mut self, source: R) {
815 unsafe {
817 self.as_mut_string().extend_from_within(source);
818 }
819 }
820
821 pub fn extend_from<S: AsRef<str>>(&mut self, string: S) {
823 self.push_str(string.as_ref());
824 }
825
826 pub fn reserve(&mut self, additional: Size) {
838 unsafe {
840 self.as_mut_string().reserve(additional.get());
841 }
842 }
843
844 pub fn reserve_exact(&mut self, additional: Size) {
859 unsafe {
861 self.as_mut_string().reserve_exact(additional.get());
862 }
863 }
864
865 pub fn try_reserve(&mut self, additional: Size) -> Result<(), TryReserveError> {
877 unsafe { self.as_mut_string().try_reserve(additional.get()) }
879 }
880
881 pub fn try_reserve_exact(&mut self, additional: Size) -> Result<(), TryReserveError> {
896 unsafe { self.as_mut_string().try_reserve_exact(additional.get()) }
898 }
899
900 pub fn shrink_to_fit(&mut self) {
902 unsafe {
904 self.as_mut_string().shrink_to_fit();
905 }
906 }
907
908 pub fn shrink_to(&mut self, capacity: Size) {
914 unsafe {
916 self.as_mut_string().shrink_to(capacity.get());
917 }
918 }
919
920 pub fn truncate(&mut self, new: Size) {
926 unsafe {
928 self.as_mut_string().truncate(new.get());
929 }
930 }
931
932 #[must_use]
934 pub fn next_empty(&self) -> bool {
935 let (character, _) = self.chars().consume();
936
937 self.len().get() - character.len_utf8() == 0
938 }
939
940 #[must_use]
944 pub fn next_non_empty(&self) -> bool {
945 !self.next_empty()
946 }
947
948 pub fn pop(&mut self) -> Option<char> {
951 self.next_non_empty()
952 .then(|| unsafe { self.as_mut_string().pop() })
954 .flatten()
955 }
956
957 #[must_use]
959 pub fn leak<'a>(self) -> &'a mut str {
960 self.into_string().leak()
961 }
962
963 #[must_use]
967 pub fn leak_non_empty<'a>(self) -> &'a mut NonEmptyStr {
968 unsafe { NonEmptyStr::from_mut_str_unchecked(self.leak()) }
970 }
971
972 pub fn insert(&mut self, index: usize, character: char) {
979 unsafe {
981 self.as_mut_string().insert(index, character);
982 }
983 }
984
985 pub fn insert_str(&mut self, index: usize, string: &str) {
991 unsafe {
993 self.as_mut_string().insert_str(index, string);
994 }
995 }
996
997 pub fn insert_from<S: AsRef<str>>(&mut self, index: usize, string: S) {
1004 self.insert_str(index, string.as_ref());
1005 }
1006
1007 pub fn remove(&mut self, index: usize) -> Option<char> {
1016 self.next_non_empty()
1017 .then(|| unsafe { self.as_mut_string().remove(index) })
1019 }
1020
1021 pub fn split_off(&mut self, at: Size) -> String {
1029 unsafe { self.as_mut_string().split_off(at.get()) }
1031 }
1032}
1033
1034impl ToOwned for NonEmptyStr {
1035 type Owned = NonEmptyString;
1036
1037 fn to_owned(&self) -> Self::Owned {
1038 self.to_non_empty_string()
1039 }
1040}
1041
1042impl NonEmptyStr {
1043 #[must_use]
1045 pub fn to_non_empty_string(&self) -> NonEmptyString {
1046 NonEmptyString::from_non_empty_str(self)
1047 }
1048
1049 #[must_use]
1051 pub fn to_non_empty_bytes(&self) -> NonEmptyByteVec {
1052 self.to_non_empty_string().into_non_empty_bytes()
1053 }
1054
1055 #[must_use]
1057 pub fn to_lowercase(&self) -> String {
1058 self.as_str().to_lowercase()
1059 }
1060
1061 #[must_use]
1063 pub fn to_uppercase(&self) -> String {
1064 self.as_str().to_uppercase()
1065 }
1066
1067 #[must_use]
1076 pub fn repeat(&self, count: Size) -> NonEmptyString {
1077 let non_empty = self.as_str().repeat(count.get());
1078
1079 unsafe { NonEmptyString::new_unchecked(non_empty) }
1081 }
1082
1083 #[must_use]
1085 pub fn to_non_empty_lowercase(&self) -> NonEmptyString {
1086 unsafe { NonEmptyString::new_unchecked(self.to_lowercase()) }
1088 }
1089
1090 #[must_use]
1092 pub fn to_non_empty_uppercase(&self) -> NonEmptyString {
1093 unsafe { NonEmptyString::new_unchecked(self.to_uppercase()) }
1095 }
1096}
1097
1098impl NonEmptyString {
1099 #[must_use]
1101 pub fn single(character: char) -> Self {
1102 unsafe { Self::new_unchecked(character.to_string()) }
1104 }
1105
1106 #[must_use]
1108 pub fn with_capacity_and_char(capacity: Size, character: char) -> Self {
1109 let mut string = String::with_capacity(capacity.get());
1110
1111 string.push(character);
1112
1113 unsafe { Self::new_unchecked(string) }
1115 }
1116}
1117
1118impl FromNonEmptyIterator<char> for NonEmptyString {
1119 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = char>>(iterable: I) -> Self {
1120 let (character, iterator) = iterable.into_non_empty_iter().consume();
1121
1122 let mut output = Self::single(character);
1123
1124 output.extend(iterator);
1125
1126 output
1127 }
1128}
1129
1130impl<'c> FromNonEmptyIterator<&'c char> for NonEmptyString {
1131 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'c 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<'s> FromNonEmptyIterator<&'s NonEmptyStr> for NonEmptyString {
1143 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = &'s NonEmptyStr>>(iterable: I) -> Self {
1144 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1145
1146 let mut output = Self::from_non_empty_str(non_empty);
1147
1148 output.extend(iterator);
1149
1150 output
1151 }
1152}
1153
1154impl FromNonEmptyIterator<Self> for NonEmptyString {
1155 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = Self>>(iterable: I) -> Self {
1156 let (mut output, iterator) = iterable.into_non_empty_iter().consume();
1157
1158 output.extend(iterator);
1159
1160 output
1161 }
1162}
1163
1164impl FromNonEmptyIterator<NonEmptyBoxedStr> for NonEmptyString {
1165 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyBoxedStr>>(iterable: I) -> Self {
1166 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1167
1168 let mut output = Self::from_non_empty_boxed_str(non_empty);
1169
1170 output.extend(iterator);
1171
1172 output
1173 }
1174}
1175
1176impl<'s> FromNonEmptyIterator<NonEmptyCowStr<'s>> for NonEmptyString {
1177 fn from_non_empty_iter<I: IntoNonEmptyIterator<Item = NonEmptyCowStr<'s>>>(
1178 iterable: I,
1179 ) -> Self {
1180 let (non_empty, iterator) = iterable.into_non_empty_iter().consume();
1181
1182 let mut output = non_empty.into_owned();
1183
1184 output.extend(iterator);
1185
1186 output
1187 }
1188}