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
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
//! A UTF-8 encoded, growable string.
//!
//! The `String2` type is string type that has owership over the [char]. 
//!
//! # Example
//!
//! You can create a `String2` from a literal string with `String2::from`:
//!
//! ```
//! use string2::String2;
//!
//! let hello = String2::from("hello, world!");
//! ```
//! 
//! You can append a [`char`] to a `String2` with the [`push`] method, and
//! append a [`&str`] with the [`push_str`] method;
//!
//! ```
//! use string2::String2;
//!
//! let mut hello = String2::from("Hello, ");
//!
//! hello.push('w');
//! hello.push_str("orld!");
//! ```
//!
//! [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
//! [`char`]: https://doc.rust-lang.org/std/primitive.char.html
//! [`push`]: #method.push
//! [`push_str`]: #method.push_str
//!
//! If you have a [`String`], you can create a `String2` from it with the
//! [`from`] method, and you can convert `String2` to [`String`] whit the
//! [`into`] method:
//!
//! ```
//! use string2::String2;
//!
//! let hello = String::from("Hello world!");
//!
//! let world = String2::from(hello);
//!
//! let hello_world: String = world.into();
//! ```
//!
//! [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
//! [`from`]: #method.from
//! [`into`]: #method.into

use std::ops;
use std::fmt;

/// A UTF-8 encoded, growable string.
///
/// The `String2` type is string type that has owership over the [char]. 
///
/// # Example
///
/// You can create a `String2` from a literal string with `String2::from`:
///
/// ```
/// use string2::String2;
///
/// let hello = String2::from("hello, world!");
/// ```
/// 
/// You can append a [`char`] to a `String2` with the [`push`] method, and
/// append a [`&str`] with the [`push_str`] method;
///
/// ```
/// use string2::String2;
///
/// let mut hello = String2::from("Hello, ");
///
/// hello.push('w');
/// hello.push_str("orld!");
/// ```
///
/// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
/// [`char`]: https://doc.rust-lang.org/std/primitive.char.html
/// [`push`]: #method.push
/// [`push_str`]: #method.push_str
///
/// If you have a [`String`], you can create a `String2` from it with the
/// [`from`] method, and you can convert `String2` to [`String`] whit the
/// [`into`] method:
///
/// ```
/// use string2::String2;
///
/// let hello = String::from("Hello world!");
///
/// let world = String2::from(hello);
///
/// let hello_world: String = world.into();
/// ```
///
/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
/// [`from`]: #method.from
/// [`into`]: #method.into
///
///
/// # Representation
///
/// A `String2` is made up of three components: a pointer to some chars, a
/// length, and a capacity. The pointer points to an internal buffer `String2`
/// uses to store its data. The length is the munber of bytes currently
/// stored in the buffer, and the capacity is the size of the buffer in
/// chars. As such, the length will always be less than or equal to the
/// capacity.
///
/// The buffer is always stored on the heap.
///
/// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]
/// methods:
///
/// ```
/// use std::mem;
/// use string2::String2;
///
/// let story = String2::from("Once upon a time...");
///
/// let ptr = story.as_ptr();
/// let len = story.len();
/// let capacity = story.capacity();
///
/// // story has nineteen chars
/// assert_eq!(19, len);
///
/// // Now that we have our parts, we throw the story away.
/// mem::forget(story);
///
/// // We can re-build a String2 out of ptr, len, and capacity. This is all
/// // unsafe because we are responsible for making sure the components
/// // valid:
/// let s = unsafe { String2::from_raw_parts(ptr as *mut _, len, capacity) };
///
/// assert_eq!(String2::from("Once upon a time..."), s);
/// ```
///
/// [`as_ptr`]: #method.as_ptr
/// [`len`]: #method.len
/// [`capacity`]: #method.capacity
///
/// If a `String2` has enough capacity, adding elements to it will not
/// re-allocate. For example, consider this program:
///
/// ```
/// use string2::String2;
///
/// let mut s = String2::new();
///
/// println!("{}", s.capacity());
///
/// for _ in 0..5 {
///     s.push_str("hello");
///     println!("{}", s.capacity());
/// }
/// ```
///
/// This will output the following:
///
/// ```text
/// 0
/// 5
/// 10
/// 20
/// 20
/// 40
/// ```
///
/// At first, we have no memory allocated at all, but as we append to the
/// string, it increases its capacity appropriately. If we instead use the
/// [`with_capacity`] method to allocate the correct capacity initially:
///
/// ```
/// use string2::String2;
///
/// let mut s = String2::with_capacity(25);
///
/// println!("{}", s.capacity());
///
/// for _ in 0..5 {
///     s.push_str("hello");
///     println!("{}", s.capacity());
/// }
/// ```
///
/// [`with_capacity`]: #method.with_capacity
///
/// We end up with a different output:
///
/// ```text
/// 25
/// 25
/// 25
/// 25
/// 25
/// 25
/// ```
///
/// Here, there's no need to allocate more memory inside the loop.
#[derive(Clone, Eq, Ord)]
pub struct String2 {
    inner: Vec<char>
}

impl String2 {
    /// Creates a new empty `String2`.
    ///
    /// Given that the `String2` is empty, this will not allocate any initial
    /// buffer. While that means that this initial operation is very
    /// inexpensive, but may cause excessive allocation later, when you add
    /// data. If you have an idea of how much data the `String2` will hold,
    /// consider the [`with_capacity`] method to prevent excessive
    /// re-allocation.
    ///
    /// [`with_capacity`]: #method.with_capacity
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::new();
    /// ```
    #[inline]
    pub fn new() -> String2 {
        String2 {
            inner: Vec::new()
        }
    }

    /// Creates a new empty `String2` with a particular capacity.
    ///
    /// `String2`s have an internal buffer to hold their data. The capacity is
    /// the length of that buffer, and can be queried with the [`capacity`]
    /// method. This method creates an empty `String2`, but one with an initial
    /// buffer that can hold `capacity` bytes. This is useful when you may be
    /// appending a bunch of data to the `String2`, reducing the number of
    /// reallocations it needs to do.
    ///
    /// [`capacity`]: #method.capacity
    ///
    /// If the given capacity is `0`, no allocation will occur, and this method
    /// is identical to the [`new`] method.
    ///
    /// [`new`]: #method.new
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::with_capacity(10);
    ///
    /// // The String2 contains no chars, even though it has capacity for more
    /// assert_eq!(s.len(), 0);
    ///
    /// // These are all done without reallocating...
    /// let cap = s.capacity();
    /// for i in 0..10 {
    ///     s.push('a');
    /// }
    ///
    /// assert_eq!(s.capacity(), cap);
    ///
    /// // ...but this may make the vector reallocate
    /// s.push('a');
    /// ```
    #[inline]
    pub fn with_capacity(capacity: usize) -> String2 {
        String2 {
            inner: Vec::with_capacity(capacity)
        }
    }

    /// Returns this `String2`'s capacity, in bytes.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::with_capacity(10);
    ///
    /// assert!(s.capacity() >= 10);
    /// ```
    #[inline]
    pub fn capacity(&self) -> usize {
        self.inner.capacity()
    }

    /// Ensures that this `String2`'s capacity is at least `additional` bytes
    /// larger than its length.
    ///
    /// The capacity may be increased by more than `additional` bytes if it
    /// chooses, to prevent frequent reallocations.
    ///
    /// If you do not want this "at least" behavior, see the [`reserve_exact`]
    /// method.
    ///
    /// [`reserve_exact`]: #method.reserve_exact
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `usize`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::new();
    ///
    /// s.reserve(10);
    ///
    /// assert!(s.capacity() >= 10);
    /// ```
    ///
    /// This may not actually increase the capacity:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::with_capacity(10);
    /// s.push('a');
    /// s.push('b');
    ///
    /// // s now has a length of 2 and a capacity of 10
    /// assert_eq!(2, s.len());
    /// assert_eq!(10, s.capacity());
    ///
    /// // Since we already have an extra 8 capacity, calling this...
    /// s.reserve(8);
    ///
    /// // ... doesn't actually increase.
    /// assert_eq!(10, s.capacity());
    /// ```
    #[inline]
    pub fn reserve(&mut self, additional: usize) {
        self.inner.reserve(additional);
    }

    /// Ensures that this `String2`'s capacity is `additional` bytes
    /// larger than its length.
    ///
    /// Consider using the [`reserve`] method unless you absolutely know
    /// better than the allocator.
    ///
    /// [`reserve`]: #method.reserve
    ///
    /// # Panics
    ///
    /// Panics if the new capacity overflows `usize`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::new();
    ///
    /// s.reserve_exact(10);
    ///
    /// assert!(s.capacity() >= 10);
    /// ```
    ///
    /// This may not actually increase the capacity:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::with_capacity(10);
    /// s.push('a');
    /// s.push('b');
    ///
    /// // s now has a length of 2 and a capacity of 10
    /// assert_eq!(2, s.len());
    /// assert_eq!(10, s.capacity());
    ///
    /// // Since we already have an extra 8 capacity, calling this...
    /// s.reserve_exact(8);
    ///
    /// // ... doesn't actually increase.
    /// assert_eq!(10, s.capacity());
    /// ```
    #[inline]
    pub fn reserve_exact(&mut self, additional: usize) {
        self.inner.reserve_exact(additional);
    }

    /// Shrinks the capacity of this `String2` to match its length.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::from("foo");
    ///
    /// s.reserve(100);
    /// assert!(s.capacity() >= 100);
    ///
    /// s.shrink_to_fit();
    /// assert_eq!(3, s.capacity());
    /// ```
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.inner.shrink_to_fit();
    }

    /// Converts a `String2` to a raw pointer.
    /// As `String2` are a vector of chars, the raw pointer points to a char.
    /// This pointer will be pointing to the first byte of the `String2`.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::from("Hello");
    /// let ptr = s.as_ptr();
    /// ```
    #[inline]
    pub fn as_ptr(&self) -> *const char {
        self.inner.as_ptr()
    }

    /// Creates a new `String2` from a length, capacity, and pointer.
    ///
    /// # Safety
    ///
    /// This is highly unsafe, due to the number of invariants that aren't
    /// checked:
    ///
    /// * The memory at `ptr` needs to have been previously allocated by the
    ///   same allocator the standard library uses.
    /// * `length` needs to be less than or equal to `capacity`.
    /// * `capacity` needs to be the correct value.
    ///
    /// Violating these may cause problems like corrupting the allocator's
    /// internal datastructures.
    ///
    /// The ownership of `ptr` is effectively transferred to the
    /// `String2` which may then deallocate, reallocate or change the
    /// contents of memory pointed to by the pointer at will. Ensure
    /// that nothing else uses the pointer after calling this
    /// function.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use std::mem;
    /// use string2::String2;
    ///
    /// let s = String2::from("hello");
    /// let ptr = s.as_ptr();
    /// let len = s.len();
    /// let capacity = s.capacity();
    ///
    /// mem::forget(s);
    ///
    /// let s = unsafe { String2::from_raw_parts(ptr as *mut _, len, capacity) };
    ///
    /// assert_eq!(String2::from("hello"), s);
    /// ```
    #[inline]
    pub unsafe fn from_raw_parts(buf: *mut char, length: usize, capacity: usize) -> String2 {
        String2 {
            inner: Vec::from_raw_parts(buf, length, capacity)
        }
    }

    /// Converts a `String2` into a byte vector.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::from("hello");
    /// let bytes = s.as_bytes();
    ///
    /// assert_eq!(&[104, 101, 108, 108, 111], &bytes[..]);
    /// ```
    #[inline]
    pub fn as_bytes(&self) -> Vec<u8> {
        let s: String = self.clone().into();
        s.into_bytes()
    }

    /// Converts a `String2` into a char slice.
    ///
    /// This consumes the `String2`, so we do not need to copy its contents.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::from("hello");
    /// let bytes = s.as_slice();
    ///
    /// assert_eq!(&['h', 'e', 'l', 'l', 'o'][..], &bytes[..]);
    /// ```
    #[inline]
    pub fn as_slice(&self) -> &[char] {
        self.inner.as_slice()
    }

    /// Converts a `String2` into a mut char slice.
    ///
    /// This consumes the `String2`, so we do not need to copy its contents.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::from("hello");
    /// {
    ///     let bytes = s.as_mut_slice();
    ///     bytes[1] = 'a';
    /// }
    ///
    /// assert_eq!(String2::from("hallo"), s);
    /// ```
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [char] {
        self.inner.as_mut_slice()
    }

    /// Converts a `String2` into a char vector.
    ///
    /// This consumes the `String2`, so we do not need to copy its contents.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let s = String2::from("hello");
    /// let bytes = s.as_vec();
    ///
    /// assert_eq!(&['h', 'e', 'l', 'l', 'o'], &bytes[..]);
    /// ```
    #[inline]
    pub fn as_vec(self) -> Vec<char> {
        self.inner
    }

    /// Converts a `String2` into a mut char slice.
    ///
    /// This consumes the `String2`, so we do not need to copy its contents.
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// use string2::String2;
    ///
    /// let mut s = String2::from("hello");
    /// {
    ///     let bytes = s.as_mut_vec();
    ///     bytes[1] = 'a';
    /// }
    ///
    /// assert_eq!(String2::from("hallo"), s);
    /// ```
    #[inline]
    pub fn as_mut_vec(&mut self) -> &mut Vec<char> {
        &mut self.inner
    }

    #[inline]
    pub fn retain<F>(&mut self, f: F)
        where F: FnMut(&char) -> bool
    {
        self.inner.retain(f)
    }

    #[inline]
    pub fn get(&self, idx: usize) -> Option<&char> {
        self.inner.get(idx)
    }

    #[inline]
    pub fn get_mut(&mut self, idx: usize) -> Option<&mut char> {
        self.inner.get_mut(idx)
    }

    #[inline]
    pub fn truncate(&mut self, new_len: usize) {
        self.inner.truncate(new_len);
    }

    #[inline]
    pub fn push(&mut self, ch: char) {
        self.inner.push(ch);
    }

    #[inline]
    pub fn push_str(&mut self, string: &str) {
        self.inner.extend(string.chars())
    }

    #[inline]
    pub fn pop(&mut self) -> Option<char> {
        self.inner.pop()
    }

    #[inline]
    pub fn remove(&mut self, idx: usize) -> char {
        self.inner.remove(idx)
    }

    #[inline]
    pub fn insert(&mut self, idx: usize, ch: char) {
        self.inner.insert(idx, ch);
    }

    #[inline]
    pub fn insert_str(&mut self, _idx: usize, _string: &str) {
        
    }

    #[inline]
    pub fn append(&mut self, other: &mut Self) {
        self.inner.append(&mut other.inner)
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.inner.is_empty()
    }

    #[inline]
    pub fn split_off(&mut self, at: usize) -> String2 {
        let other = self.inner.split_off(at);

        String2 {
            inner: other
        }
    }

    #[inline]
    pub fn split_at(&self, mid: usize) -> (String2, String2) {
        let (a, b) = self.inner.split_at(mid);

        (String2 { inner: a.to_vec() }, String2 { inner: b.to_vec() })
    }

    #[inline]
    pub fn clear(&mut self) {
        self.inner.clear()
    }

    #[inline]
    pub fn iter(self) -> StrIterator {
        self.into_iter()
    }
}

impl<'a> From<&'a str> for String2 {
    #[inline]
    fn from(string: &'a str) -> String2 {
        String2 {
            inner: string.chars().collect()
        }
    }
}

impl From<String> for String2 {
    #[inline]
    fn from(string: String) -> String2 {
        String2 {
            inner: string.chars().collect()
        }
    }
}

impl From<Vec<char>> for String2 {
    #[inline]
    fn from(s: Vec<char>) -> String2 {
        String2 {
            inner: s
        }
    }
}

impl<'a> From<&'a [char]> for String2 {
    #[inline]
    fn from(s: &'a [char]) -> String2 {
        String2 {
            inner: s.to_vec()
        }
    }
}

impl<'a> From<&'a mut [char]> for String2 {
    #[inline]
    fn from(s: &'a mut [char]) -> String2 {
        String2 {
            inner: s.to_vec()
        }
    }
}

impl Into<String> for String2 {
    fn into(self) -> String {
        self.inner.iter().map(|c| c.encode_utf8(&mut [0; 4]).to_string()).collect()
    }
}

impl<'a> Into<String> for &'a String2 {
    fn into(self) -> String {
        self.inner.iter().map(|c| c.encode_utf8(&mut [0; 4]).to_string()).collect()
    }
}

impl Default for String2 {
    #[inline]
    fn default() -> String2 {
        String2::new()
    }
}

impl IntoIterator for String2 {
    type Item = char;
    type IntoIter = StrIterator;
    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        StrIterator {
            inner: self.inner.into_iter()
        }
    }
}

pub struct StrIterator {
    inner: ::std::vec::IntoIter<char>
}

impl Iterator for StrIterator {
    type Item = char;
    #[inline]
    fn next(&mut self) -> Option<char> {
        self.inner.next()
    }
}

impl AsRef<String2> for String2 {
    #[inline]
    fn as_ref(&self) -> &String2 {
        self
    }
}

impl AsMut<String2> for String2 {
    #[inline]
    fn as_mut(&mut self) -> &mut String2 {
        self
    }
}

impl AsRef<[char]> for String2 {
    #[inline]
    fn as_ref(&self) -> &[char] {
        &self.inner
    }
}

impl AsMut<[char]> for String2 {
    #[inline]
    fn as_mut(&mut self) -> &mut [char] {
        &mut self.inner
    }
}

impl ops::Add for String2 {
    type Output = String2;
    #[inline]
    fn add(self, other: String2) -> String2 {
        let mut self2 = self;
        let mut other = other;
        self2.inner.append(&mut other.inner);
        self2
    }
}

impl ops::Add<char> for String2 {
    type Output = String2;
    #[inline]
    fn add(mut self, other: char) -> String2 {
        self.push(other);
        self
    }
}

impl<'a> ops::Add<&'a str> for String2 {
    type Output = String2;
    #[inline]
    fn add(mut self, other: &str) -> String2 {
        self.push_str(other);
        self
    }
}

impl ops::AddAssign for String2 {
    #[inline]
    fn add_assign(&mut self, other: String2) {
        let mut other = other;
        self.inner.append(other.inner.as_mut())
    }
}

impl ops::AddAssign<char> for String2 {
    #[inline]
    fn add_assign(&mut self, other: char) {
        self.push(other)
    }
}

impl<'a> ops::AddAssign<&'a str> for String2 {
    #[inline]
    fn add_assign(&mut self, other: &str) {
        self.push_str(other)
    }
}

impl PartialEq for String2 {
    #[inline]
    fn eq(&self, other: &String2) -> bool {
        self.inner == other.inner
    }
}

impl PartialOrd for String2 {
    #[inline]
    fn partial_cmp(&self, other: &String2) -> Option<::std::cmp::Ordering> {
        PartialOrd::partial_cmp(&self.inner, &other.inner)
    }
}

impl ops::Index<usize> for String2 {
    type Output = char;
    #[inline]
    fn index(&self, idx: usize) -> &char {
        &self.inner[idx]
    }
}

impl ops::Index<ops::Range<usize>> for String2 {
    type Output = [char];
    #[inline]
    fn index(&self, range: ops::Range<usize>) -> &[char] {
        self.inner.index(range)
    }
}

impl ops::Index<ops::RangeFrom<usize>> for String2 {
    type Output = [char];
    #[inline]
    fn index(&self, range: ops::RangeFrom<usize>) -> &[char] {
        self.inner.index(range)
    }
}

impl ops::Index<ops::RangeTo<usize>> for String2 {
    type Output = [char];
    #[inline]
    fn index(&self, range: ops::RangeTo<usize>) -> &[char] {
        self.inner.index(range)
    }
}

impl ops::Index<ops::RangeFull> for String2 {
    type Output = [char];
    #[inline]
    fn index(&self, _range: ops::RangeFull) -> &[char] {
        self.as_ref()
    }
}

impl ops::IndexMut<usize> for String2 {
    #[inline]
    fn index_mut(&mut self, idx: usize) -> &mut char {
        &mut self.inner[idx]
    }
}

impl ops::IndexMut<ops::Range<usize>> for String2 {
    #[inline]
    fn index_mut(&mut self, range: ops::Range<usize>) -> &mut [char] {
        self.inner.index_mut(range)
    }
}

impl ops::IndexMut<ops::RangeFrom<usize>> for String2 {
    #[inline]
    fn index_mut(&mut self, range: ops::RangeFrom<usize>) -> &mut [char] {
        self.inner.index_mut(range)
    }
}

impl ops::IndexMut<ops::RangeTo<usize>> for String2 {
    #[inline]
    fn index_mut(&mut self, range: ops::RangeTo<usize>) -> &mut [char] {
        self.inner.index_mut(range)
    }
}

impl ops::IndexMut<ops::RangeFull> for String2 {
    #[inline]
    fn index_mut(&mut self, range: ops::RangeFull) -> &mut [char] {
        self.inner.index_mut(range)
    }
}

impl fmt::Display for String2 {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s: String = self.into();
        fmt::Display::fmt(&s, f)
    }
}

impl fmt::Debug for String2 {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s: String = self.into();
        fmt::Debug::fmt(&s, f)
    }
}