mitsein 0.9.0

Strongly typed APIs for non-empty collections, slices, and iterators.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! A non-empty [`String`][`string`].
//!
//! [`string`]: alloc::string

#![cfg(feature = "alloc")]
#![cfg_attr(docsrs, doc(cfg(feature = "alloc")))]

use alloc::borrow::{Borrow, BorrowMut, Cow};
use alloc::string::{FromUtf8Error, FromUtf16Error, String};
#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
use core::fmt::{self, Debug, Display, Formatter, Write};
use core::mem;
use core::num::NonZeroUsize;
use core::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut};
use core::slice::SliceIndex;
#[cfg(feature = "schemars")]
use schemars::{JsonSchema, Schema, SchemaGenerator};

use crate::borrow1::{CowStr1, CowStr1Ext as _};
use crate::boxed1::{BoxedStr1, BoxedStr1Ext as _};
use crate::iter1::{Extend1, FromIterator1, IntoIterator1};
use crate::safety::{NonZeroExt as _, OptionExt as _};
use crate::slice1::Slice1;
use crate::str1::Str1;
use crate::take;
use crate::vec1::Vec1;
use crate::{Cardinality, EmptyError, FromMaybeEmpty, MaybeEmpty, NonEmpty};

impl Add<&Str1> for String {
    type Output = String;

    fn add(mut self, rhs: &Str1) -> Self::Output {
        self.push_str(rhs);
        self
    }
}

impl AddAssign<&Str1> for String {
    fn add_assign(&mut self, rhs: &Str1) {
        self.push_str(rhs);
    }
}

impl<'a> Extend<&'a Str1> for String {
    fn extend<I>(&mut self, items: I)
    where
        I: IntoIterator<Item = &'a Str1>,
    {
        self.extend(items.into_iter().map(Str1::as_str))
    }
}

impl Extend<String1> for String {
    fn extend<I>(&mut self, items: I)
    where
        I: IntoIterator<Item = String1>,
    {
        self.extend(items.into_iter().map(String1::into_string))
    }
}

impl Extend1<char> for String {
    fn extend_non_empty<I>(mut self, items: I) -> String1
    where
        I: IntoIterator1<Item = char>,
    {
        self.extend(items);
        // SAFETY: The input iterator `items` is non-empty and `extend` either pushes one or more
        //         items or panics, so `self` must be non-empty here.
        unsafe { String1::from_maybe_empty_unchecked(self) }
    }
}

unsafe impl MaybeEmpty for String {
    fn cardinality(&self) -> Option<Cardinality<(), ()>> {
        self.as_str().cardinality()
    }
}

type TakeIfMany<'a, N = ()> = take::TakeIfMany<'a, String, char, N>;

pub type PopIfMany<'a> = TakeIfMany<'a, ()>;

pub type RemoveIfMany<'a> = TakeIfMany<'a, usize>;

impl<N> TakeIfMany<'_, N> {
    pub fn or_get_only(self) -> Result<char, char> {
        self.take_or_else(|items, _| items.first())
    }

    pub fn or_replace_only(self, replacement: char) -> Result<char, char> {
        self.or_else_replace_only(move || replacement)
    }

    pub fn or_else_replace_only<F>(self, f: F) -> Result<char, char>
    where
        F: FnOnce() -> char,
    {
        self.take_or_else(move |items, _| {
            let target = items.first();
            items.items.clear();
            items.items.push(f());
            target
        })
    }
}

impl TakeIfMany<'_, usize> {
    pub fn or_get(self) -> Result<char, char> {
        self.take_or_else(|items, index| {
            if items.is_char_boundary(index) {
                items.first()
            }
            else {
                self::panic_index_is_not_char_boundary()
            }
        })
    }

    pub fn or_replace(self, replacement: char) -> Result<char, char> {
        self.or_else_replace(move || replacement)
    }

    pub fn or_else_replace<F>(self, f: F) -> Result<char, char>
    where
        F: FnOnce() -> char,
    {
        self.take_or_else(move |items, index| {
            if items.is_char_boundary(index) {
                let target = items.items.remove(index);
                items.items.push(f());
                target
            }
            else {
                self::panic_index_is_not_char_boundary()
            }
        })
    }
}

pub type String1 = NonEmpty<String>;

impl String1 {
    /// # Safety
    ///
    /// `items` must be non-empty. For example, it is unsound to call this function with the
    /// immediate output of [`Vec::new()`][`Vec::new`].
    ///
    /// [`Vec::new`]: alloc::vec::Vec::new
    pub unsafe fn from_string_unchecked(items: String) -> Self {
        unsafe { FromMaybeEmpty::from_maybe_empty_unchecked(items) }
    }

    pub fn from_one_with_capacity<U>(item: char, capacity: usize) -> Self {
        String1::from_iter1_with_capacity([item], capacity)
    }

    pub fn from_iter1_with_capacity<U>(items: U, capacity: usize) -> Self
    where
        String: Extend1<U::Item>,
        U: IntoIterator1,
    {
        String::with_capacity(capacity).extend_non_empty(items)
    }

    pub fn try_from_ref(items: &String) -> Result<&'_ Self, EmptyError<&'_ String>> {
        items.try_into()
    }

    pub fn try_from_mut(items: &mut String) -> Result<&'_ mut Self, EmptyError<&'_ mut String>> {
        items.try_into()
    }

    pub fn from_utf8(items: Vec1<u8>) -> Result<Self, FromUtf8Error> {
        // SAFETY: `items` is non-empty and `String::from_utf8` checks for valid UTF-8, so there
        //         must be one or more code points.
        String::from_utf8(items.into_vec())
            .map(|items| unsafe { String1::from_string_unchecked(items) })
    }

    pub fn from_utf8_lossy(items: &Slice1<u8>) -> CowStr1<'_> {
        // SAFETY: `items` is non-empty and `String::from_utf8_lossy` checks for valid UTF-8 or
        //         introduces replacement characters, so there must be one or more code points.
        unsafe {
            match String::from_utf8_lossy(items.as_slice()) {
                Cow::Borrowed(items) => Cow::Borrowed(Str1::from_str_unchecked(items)),
                Cow::Owned(items) => Cow::Owned(String1::from_string_unchecked(items)),
            }
        }
    }

    pub fn from_utf16(items: &Slice1<u16>) -> Result<Self, FromUtf16Error> {
        // SAFETY: `items` is non-empty and `String::from_utf16` checks for valid UTF-16, so there
        //         must be one or more code points.
        String::from_utf16(items.as_slice())
            .map(|items| unsafe { String1::from_string_unchecked(items) })
    }

    pub fn from_utf16_lossy(items: &Slice1<u16>) -> String1 {
        // SAFETY: `items` is non-empty and `String::from_utf16_lossy` checks for valid UTF-16 or
        //         introduces replacement characters, so there must be one or more code points.
        unsafe { String1::from_string_unchecked(String::from_utf16_lossy(items.as_slice())) }
    }

    pub fn into_string(self) -> String {
        self.items
    }

    pub fn into_boxed_str1(self) -> BoxedStr1 {
        // SAFETY: `self` must be non-empty.
        unsafe { BoxedStr1::from_boxed_str_unchecked(self.items.into_boxed_str()) }
    }

    pub fn leak<'a>(self) -> &'a mut Str1 {
        // SAFETY: `self` must be non-empty.
        unsafe { Str1::from_mut_str_unchecked(self.items.leak()) }
    }

    pub fn try_retain<F>(self, f: F) -> Result<Self, EmptyError<String>>
    where
        F: FnMut(char) -> bool,
    {
        self.and_then_try(|items| items.retain(f))
    }

    pub fn reserve(&mut self, additional: usize) {
        self.items.reserve(additional)
    }

    pub fn reserve_exact(&mut self, additional: usize) {
        self.items.reserve_exact(additional)
    }

    pub fn shrink_to(&mut self, capacity: usize) {
        self.items.shrink_to(capacity)
    }

    pub fn shrink_to_fit(&mut self) {
        self.items.shrink_to_fit()
    }

    pub fn split_off_tail(&mut self) -> String {
        let index = unsafe {
            // SAFETY: `self` must be non-empty.
            self.items
                .char_indices()
                .take(2)
                .last()
                .map(|(index, _)| index)
                .unwrap_maybe_unchecked()
        };
        self.items.split_off(index)
    }

    pub fn push(&mut self, item: char) {
        self.items.push(item)
    }

    pub fn push_str(&mut self, items: &str) {
        self.items.push_str(items)
    }

    pub fn pop_if_many(&mut self) -> PopIfMany<'_> {
        // SAFETY: `with` executes this closure only if `self` contains more than one item.
        TakeIfMany::with(self, (), |items, ()| unsafe {
            items.items.pop().unwrap_maybe_unchecked()
        })
    }

    pub fn insert(&mut self, index: usize, item: char) {
        self.items.insert(index, item)
    }

    pub fn remove_if_many(&mut self, index: usize) -> RemoveIfMany<'_> {
        TakeIfMany::with(self, index, |items, index| items.items.remove(index))
    }

    pub fn len(&self) -> NonZeroUsize {
        // SAFETY: `self` must be non-empty.
        unsafe { NonZeroUsize::new_maybe_unchecked(self.items.len()) }
    }

    pub fn capacity(&self) -> NonZeroUsize {
        // SAFETY: `self` must be non-empty.
        unsafe { NonZeroUsize::new_maybe_unchecked(self.items.capacity()) }
    }

    pub const fn as_string(&self) -> &String {
        &self.items
    }

    /// # Safety
    ///
    /// The [`String`] behind the returned mutable reference **must not** be empty when the
    /// reference is dropped. Consider the following example:
    ///
    /// ```rust,no_run
    /// use mitsein::string1::String1;
    ///
    /// let mut xs = String1::try_from("abc").unwrap();
    /// // This block is unsound. The `&mut String` is dropped in the block and so `xs` can be
    /// // freely manipulated after the block despite violation of the non-empty guarantee.
    /// unsafe {
    ///     xs.as_mut_string().clear();
    /// }
    /// let x = xs.as_bytes1().first(); // Undefined behavior!
    /// ```
    pub const unsafe fn as_mut_string(&mut self) -> &mut String {
        &mut self.items
    }

    /// # Safety
    ///
    /// The returned [`Vec1`] must contain valid UTF-8 when the reference is dropped. Note that the
    /// non-empty guarantee of `String1` may also be violated by invalid UTF-8, because invalid
    /// UTF-8 bytes may yield no code points.
    pub unsafe fn as_mut_vec1(&mut self) -> &mut Vec1<u8> {
        unsafe { mem::transmute(self.items.as_mut_vec()) }
    }

    pub fn as_str1(&self) -> &Str1 {
        // SAFETY: `self` must be non-empty.
        unsafe { Str1::from_str_unchecked(self.items.as_str()) }
    }

    pub fn as_mut_str1(&mut self) -> &mut Str1 {
        // SAFETY: `self` must be non-empty.
        unsafe { Str1::from_mut_str_unchecked(self.items.as_mut_str()) }
    }

    pub fn as_ptr(&self) -> *const u8 {
        self.items.as_ptr()
    }

    pub fn as_mut_ptr(&mut self) -> *mut u8 {
        self.items.as_mut_ptr()
    }
}

impl Add<&str> for String1 {
    type Output = String1;

    fn add(mut self, rhs: &str) -> Self::Output {
        self.push_str(rhs);
        self
    }
}

impl Add<&Str1> for String1 {
    type Output = String1;

    fn add(mut self, rhs: &Str1) -> Self::Output {
        self.push_str(rhs);
        self
    }
}

impl AddAssign<&str> for String1 {
    fn add_assign(&mut self, rhs: &str) {
        self.push_str(rhs);
    }
}

impl AddAssign<&Str1> for String1 {
    fn add_assign(&mut self, rhs: &Str1) {
        self.push_str(rhs);
    }
}

#[cfg(feature = "arbitrary")]
#[cfg_attr(docsrs, doc(cfg(feature = "arbitrary")))]
impl<'a> Arbitrary<'a> for String1 {
    fn arbitrary(unstructured: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
        <&'a Str1>::arbitrary(unstructured).map(String1::from)
    }

    fn size_hint(depth: usize) -> (usize, Option<usize>) {
        (<&'a Str1>::size_hint(depth).0, None)
    }
}

impl AsMut<str> for String1 {
    fn as_mut(&mut self) -> &mut str {
        self.items.as_mut()
    }
}

impl AsMut<Str1> for String1 {
    fn as_mut(&mut self) -> &mut Str1 {
        self.as_mut_str1()
    }
}

impl AsRef<str> for String1 {
    fn as_ref(&self) -> &str {
        self.items.as_ref()
    }
}

impl AsRef<Str1> for String1 {
    fn as_ref(&self) -> &Str1 {
        self.as_str1()
    }
}

impl Borrow<str> for String1 {
    fn borrow(&self) -> &str {
        self.items.borrow()
    }
}

impl Borrow<Str1> for String1 {
    fn borrow(&self) -> &Str1 {
        self.as_str1()
    }
}

impl BorrowMut<str> for String1 {
    fn borrow_mut(&mut self) -> &mut str {
        self.items.borrow_mut()
    }
}

impl BorrowMut<Str1> for String1 {
    fn borrow_mut(&mut self) -> &mut Str1 {
        self.as_mut_str1()
    }
}

impl Debug for String1 {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        write!(formatter, "{:?}", &self.items)
    }
}

impl Deref for String1 {
    type Target = Str1;

    fn deref(&self) -> &Self::Target {
        self.as_str1()
    }
}

impl DerefMut for String1 {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.as_mut_str1()
    }
}

impl Display for String1 {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        write!(formatter, "{}", &self.items)
    }
}

// This unfortunately cannot support extending from `CowStr1`s, because `Extend<CowStr1<'_>>`
// cannot be implemented for `String` in this crate. It cannot be implemented directly for
// `String1` either, because it conflicts with this implementation.
impl<T> Extend<T> for String1
where
    String: Extend<T>,
{
    fn extend<I>(&mut self, extension: I)
    where
        I: IntoIterator<Item = T>,
    {
        self.items.extend(extension)
    }
}

impl From<BoxedStr1> for String1 {
    fn from(items: BoxedStr1) -> Self {
        // SAFETY: `items` must be non-empty.
        unsafe { String1::from_string_unchecked(String::from(items.into_boxed_str())) }
    }
}

impl From<char> for String1 {
    fn from(point: char) -> Self {
        // SAFETY: The `From<char>` implementation for `String` never constructs an empty `String`.
        unsafe { String1::from_string_unchecked(String::from(point)) }
    }
}

impl<'a> From<CowStr1<'a>> for String1 {
    fn from(items: CowStr1<'a>) -> Self {
        items.into_owned()
    }
}

impl<'a> From<&'a Str1> for String1 {
    fn from(items: &'a Str1) -> Self {
        // SAFETY: `items` must be non-empty.
        unsafe { String1::from_string_unchecked(String::from(items.as_str())) }
    }
}

impl<'a> From<&'a mut Str1> for String1 {
    fn from(items: &'a mut Str1) -> Self {
        // SAFETY: `items` must be non-empty.
        unsafe { String1::from_string_unchecked(String::from(items.as_str())) }
    }
}

impl From<String1> for String {
    fn from(items: String1) -> Self {
        items.items
    }
}

impl FromIterator1<char> for String1 {
    fn from_iter1<I>(items: I) -> Self
    where
        I: IntoIterator1<Item = char>,
    {
        // SAFETY: `items` is non-empty and each item (`char`) is intrinsically non-empty. A
        //         `String` constructed from one or more `char`s is never empty.
        unsafe { String1::from_string_unchecked(items.into_iter().collect()) }
    }
}

impl<'a> FromIterator1<&'a char> for String1 {
    fn from_iter1<I>(items: I) -> Self
    where
        I: IntoIterator1<Item = &'a char>,
    {
        String1::from_iter1(items.into_iter1().cloned())
    }
}

impl<'a> FromIterator1<CowStr1<'a>> for String1 {
    fn from_iter1<I>(items: I) -> Self
    where
        I: IntoIterator1<Item = CowStr1<'a>>,
    {
        let (head, tail) = items.into_iter1().into_head_and_tail();
        let mut head = head.into_owned();
        head.items.extend(tail.map(CowStr1::into_cow_str));
        head
    }
}

impl<'a> FromIterator1<&'a Str1> for String1 {
    fn from_iter1<I>(items: I) -> Self
    where
        I: IntoIterator1<Item = &'a Str1>,
    {
        let (head, tail) = items.into_iter1().into_head_and_tail();
        let mut head = String1::from(head);
        head.extend(tail);
        head
    }
}

impl FromIterator1<String1> for String1 {
    fn from_iter1<I>(items: I) -> Self
    where
        I: IntoIterator1<Item = String1>,
    {
        let (mut head, tail) = items.into_iter1().into_head_and_tail();
        head.extend(tail);
        head
    }
}

impl<I> Index<I> for String1
where
    I: SliceIndex<str>,
{
    type Output = I::Output;

    fn index(&self, at: I) -> &Self::Output {
        self.items.index(at)
    }
}

impl<I> IndexMut<I> for String1
where
    I: SliceIndex<str>,
{
    fn index_mut(&mut self, at: I) -> &mut Self::Output {
        self.items.index_mut(at)
    }
}

#[cfg(feature = "schemars")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
impl JsonSchema for String1 {
    fn schema_name() -> Cow<'static, str> {
        String::schema_name()
    }

    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
        use crate::schemars;

        schemars::json_subschema_with_non_empty_property_for::<String>(
            schemars::NON_EMPTY_KEY_STRING,
            generator,
        )
    }

    fn inline_schema() -> bool {
        String::inline_schema()
    }

    fn schema_id() -> Cow<'static, str> {
        String::schema_id()
    }
}

crate::impl_partial_eq_for_non_empty!([in str] <= [in String1]);
crate::impl_partial_eq_for_non_empty!([in &str] <= [in String1]);
crate::impl_partial_eq_for_non_empty!([in &Str1] == [in String1]);
crate::impl_partial_eq_for_non_empty!([in CowStr1<'_>] == [in String1]);
crate::impl_partial_eq_for_non_empty!([in String1] => [in str]);
crate::impl_partial_eq_for_non_empty!([in String1] => [in &str]);
crate::impl_partial_eq_for_non_empty!([in String1] == [in &Str1]);

impl<'a> TryFrom<&'a str> for String1 {
    type Error = EmptyError<&'a str>;

    fn try_from(items: &'a str) -> Result<Self, Self::Error> {
        Str1::try_from_str(items).map(String1::from)
    }
}

impl<'a> TryFrom<&'a mut str> for String1 {
    type Error = EmptyError<&'a mut str>;

    fn try_from(items: &'a mut str) -> Result<Self, Self::Error> {
        Str1::try_from_mut_str(items).map(String1::from)
    }
}

impl TryFrom<String> for String1 {
    type Error = EmptyError<String>;

    fn try_from(items: String) -> Result<Self, Self::Error> {
        FromMaybeEmpty::try_from_maybe_empty(items)
    }
}

impl<'a> TryFrom<&'a String> for &'a String1 {
    type Error = EmptyError<&'a String>;

    fn try_from(items: &'a String) -> Result<Self, Self::Error> {
        FromMaybeEmpty::try_from_maybe_empty(items)
    }
}

impl<'a> TryFrom<&'a mut String> for &'a mut String1 {
    type Error = EmptyError<&'a mut String>;

    fn try_from(items: &'a mut String) -> Result<Self, Self::Error> {
        FromMaybeEmpty::try_from_maybe_empty(items)
    }
}

impl TryFrom<Vec1<u8>> for String1 {
    type Error = FromUtf8Error;

    fn try_from(items: Vec1<u8>) -> Result<Self, Self::Error> {
        String1::from_utf8(items)
    }
}

impl Write for String1 {
    fn write_str(&mut self, items: &str) -> fmt::Result {
        self.items.write_str(items)
    }

    fn write_char(&mut self, item: char) -> fmt::Result {
        self.items.write_char(item)
    }
}

const fn panic_index_is_not_char_boundary() -> ! {
    panic!("index is not at a UTF-8 code point boundary")
}

#[cfg(test)]
pub mod harness {
    use rstest::fixture;

    use crate::iter1;
    use crate::string1::String1;

    #[fixture]
    pub fn xs1(#[default(4)] end: u8) -> String1 {
        iter1::one('x')
            .first_and_then_take(usize::from(end))
            .collect1()
    }
}

#[cfg(all(test, feature = "schemars"))]
mod tests {
    use rstest::rstest;

    use crate::schemars;
    use crate::string1::String1;

    #[rstest]
    fn string1_json_schema_has_non_empty_property() {
        schemars::harness::assert_json_schema_has_non_empty_property::<String1>(
            schemars::NON_EMPTY_KEY_STRING,
        );
    }
}