string2/
lib.rs

1//! A UTF-8 encoded, growable string.
2//!
3//! The `String2` type is string type that has owership over the [char]. 
4//!
5//! # Example
6//!
7//! You can create a `String2` from a literal string with `String2::from`:
8//!
9//! ```
10//! use string2::String2;
11//!
12//! let hello = String2::from("hello, world!");
13//! ```
14//! 
15//! You can append a [`char`] to a `String2` with the [`push`] method, and
16//! append a [`&str`] with the [`push_str`] method;
17//!
18//! ```
19//! use string2::String2;
20//!
21//! let mut hello = String2::from("Hello, ");
22//!
23//! hello.push('w');
24//! hello.push_str("orld!");
25//! ```
26//!
27//! [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
28//! [`char`]: https://doc.rust-lang.org/std/primitive.char.html
29//! [`push`]: #method.push
30//! [`push_str`]: #method.push_str
31//!
32//! If you have a [`String`], you can create a `String2` from it with the
33//! [`from`] method, and you can convert `String2` to [`String`] whit the
34//! [`into`] method:
35//!
36//! ```
37//! use string2::String2;
38//!
39//! let hello = String::from("Hello world!");
40//!
41//! let world = String2::from(hello);
42//!
43//! let hello_world: String = world.into();
44//! ```
45//!
46//! [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
47//! [`from`]: #method.from
48//! [`into`]: #method.into
49
50use std::ops;
51use std::fmt;
52
53/// A UTF-8 encoded, growable string.
54///
55/// The `String2` type is string type that has owership over the [char]. 
56///
57/// # Example
58///
59/// You can create a `String2` from a literal string with `String2::from`:
60///
61/// ```
62/// use string2::String2;
63///
64/// let hello = String2::from("hello, world!");
65/// ```
66/// 
67/// You can append a [`char`] to a `String2` with the [`push`] method, and
68/// append a [`&str`] with the [`push_str`] method;
69///
70/// ```
71/// use string2::String2;
72///
73/// let mut hello = String2::from("Hello, ");
74///
75/// hello.push('w');
76/// hello.push_str("orld!");
77/// ```
78///
79/// [`&str`]: https://doc.rust-lang.org/std/primitive.str.html
80/// [`char`]: https://doc.rust-lang.org/std/primitive.char.html
81/// [`push`]: #method.push
82/// [`push_str`]: #method.push_str
83///
84/// If you have a [`String`], you can create a `String2` from it with the
85/// [`from`] method, and you can convert `String2` to [`String`] whit the
86/// [`into`] method:
87///
88/// ```
89/// use string2::String2;
90///
91/// let hello = String::from("Hello world!");
92///
93/// let world = String2::from(hello);
94///
95/// let hello_world: String = world.into();
96/// ```
97///
98/// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html
99/// [`from`]: #method.from
100/// [`into`]: #method.into
101///
102///
103/// # Representation
104///
105/// A `String2` is made up of three components: a pointer to some chars, a
106/// length, and a capacity. The pointer points to an internal buffer `String2`
107/// uses to store its data. The length is the munber of bytes currently
108/// stored in the buffer, and the capacity is the size of the buffer in
109/// chars. As such, the length will always be less than or equal to the
110/// capacity.
111///
112/// The buffer is always stored on the heap.
113///
114/// You can look at these with the [`as_ptr`], [`len`], and [`capacity`]
115/// methods:
116///
117/// ```
118/// use std::mem;
119/// use string2::String2;
120///
121/// let story = String2::from("Once upon a time...");
122///
123/// let ptr = story.as_ptr();
124/// let len = story.len();
125/// let capacity = story.capacity();
126///
127/// // story has nineteen chars
128/// assert_eq!(19, len);
129///
130/// // Now that we have our parts, we throw the story away.
131/// mem::forget(story);
132///
133/// // We can re-build a String2 out of ptr, len, and capacity. This is all
134/// // unsafe because we are responsible for making sure the components
135/// // valid:
136/// let s = unsafe { String2::from_raw_parts(ptr as *mut _, len, capacity) };
137///
138/// assert_eq!(String2::from("Once upon a time..."), s);
139/// ```
140///
141/// [`as_ptr`]: #method.as_ptr
142/// [`len`]: #method.len
143/// [`capacity`]: #method.capacity
144///
145/// If a `String2` has enough capacity, adding elements to it will not
146/// re-allocate. For example, consider this program:
147///
148/// ```
149/// use string2::String2;
150///
151/// let mut s = String2::new();
152///
153/// println!("{}", s.capacity());
154///
155/// for _ in 0..5 {
156///     s.push_str("hello");
157///     println!("{}", s.capacity());
158/// }
159/// ```
160///
161/// This will output the following:
162///
163/// ```text
164/// 0
165/// 5
166/// 10
167/// 20
168/// 20
169/// 40
170/// ```
171///
172/// At first, we have no memory allocated at all, but as we append to the
173/// string, it increases its capacity appropriately. If we instead use the
174/// [`with_capacity`] method to allocate the correct capacity initially:
175///
176/// ```
177/// use string2::String2;
178///
179/// let mut s = String2::with_capacity(25);
180///
181/// println!("{}", s.capacity());
182///
183/// for _ in 0..5 {
184///     s.push_str("hello");
185///     println!("{}", s.capacity());
186/// }
187/// ```
188///
189/// [`with_capacity`]: #method.with_capacity
190///
191/// We end up with a different output:
192///
193/// ```text
194/// 25
195/// 25
196/// 25
197/// 25
198/// 25
199/// 25
200/// ```
201///
202/// Here, there's no need to allocate more memory inside the loop.
203#[derive(Clone, Eq, Ord)]
204pub struct String2 {
205    inner: Vec<char>
206}
207
208impl String2 {
209    /// Creates a new empty `String2`.
210    ///
211    /// Given that the `String2` is empty, this will not allocate any initial
212    /// buffer. While that means that this initial operation is very
213    /// inexpensive, but may cause excessive allocation later, when you add
214    /// data. If you have an idea of how much data the `String2` will hold,
215    /// consider the [`with_capacity`] method to prevent excessive
216    /// re-allocation.
217    ///
218    /// [`with_capacity`]: #method.with_capacity
219    ///
220    /// # Examples
221    ///
222    /// Basic usage:
223    ///
224    /// ```
225    /// use string2::String2;
226    ///
227    /// let s = String2::new();
228    /// ```
229    #[inline]
230    pub fn new() -> String2 {
231        String2 {
232            inner: Vec::new()
233        }
234    }
235
236    /// Creates a new empty `String2` with a particular capacity.
237    ///
238    /// `String2`s have an internal buffer to hold their data. The capacity is
239    /// the length of that buffer, and can be queried with the [`capacity`]
240    /// method. This method creates an empty `String2`, but one with an initial
241    /// buffer that can hold `capacity` bytes. This is useful when you may be
242    /// appending a bunch of data to the `String2`, reducing the number of
243    /// reallocations it needs to do.
244    ///
245    /// [`capacity`]: #method.capacity
246    ///
247    /// If the given capacity is `0`, no allocation will occur, and this method
248    /// is identical to the [`new`] method.
249    ///
250    /// [`new`]: #method.new
251    ///
252    /// # Examples
253    ///
254    /// Basic usage:
255    ///
256    /// ```
257    /// use string2::String2;
258    ///
259    /// let mut s = String2::with_capacity(10);
260    ///
261    /// // The String2 contains no chars, even though it has capacity for more
262    /// assert_eq!(s.len(), 0);
263    ///
264    /// // These are all done without reallocating...
265    /// let cap = s.capacity();
266    /// for i in 0..10 {
267    ///     s.push('a');
268    /// }
269    ///
270    /// assert_eq!(s.capacity(), cap);
271    ///
272    /// // ...but this may make the vector reallocate
273    /// s.push('a');
274    /// ```
275    #[inline]
276    pub fn with_capacity(capacity: usize) -> String2 {
277        String2 {
278            inner: Vec::with_capacity(capacity)
279        }
280    }
281
282    /// Returns this `String2`'s capacity, in bytes.
283    ///
284    /// # Examples
285    ///
286    /// Basic usage:
287    ///
288    /// ```
289    /// use string2::String2;
290    ///
291    /// let s = String2::with_capacity(10);
292    ///
293    /// assert!(s.capacity() >= 10);
294    /// ```
295    #[inline]
296    pub fn capacity(&self) -> usize {
297        self.inner.capacity()
298    }
299
300    /// Ensures that this `String2`'s capacity is at least `additional` bytes
301    /// larger than its length.
302    ///
303    /// The capacity may be increased by more than `additional` bytes if it
304    /// chooses, to prevent frequent reallocations.
305    ///
306    /// If you do not want this "at least" behavior, see the [`reserve_exact`]
307    /// method.
308    ///
309    /// [`reserve_exact`]: #method.reserve_exact
310    ///
311    /// # Panics
312    ///
313    /// Panics if the new capacity overflows `usize`.
314    ///
315    /// # Examples
316    ///
317    /// Basic usage:
318    ///
319    /// ```
320    /// use string2::String2;
321    ///
322    /// let mut s = String2::new();
323    ///
324    /// s.reserve(10);
325    ///
326    /// assert!(s.capacity() >= 10);
327    /// ```
328    ///
329    /// This may not actually increase the capacity:
330    ///
331    /// ```
332    /// use string2::String2;
333    ///
334    /// let mut s = String2::with_capacity(10);
335    /// s.push('a');
336    /// s.push('b');
337    ///
338    /// // s now has a length of 2 and a capacity of 10
339    /// assert_eq!(2, s.len());
340    /// assert_eq!(10, s.capacity());
341    ///
342    /// // Since we already have an extra 8 capacity, calling this...
343    /// s.reserve(8);
344    ///
345    /// // ... doesn't actually increase.
346    /// assert_eq!(10, s.capacity());
347    /// ```
348    #[inline]
349    pub fn reserve(&mut self, additional: usize) {
350        self.inner.reserve(additional);
351    }
352
353    /// Ensures that this `String2`'s capacity is `additional` bytes
354    /// larger than its length.
355    ///
356    /// Consider using the [`reserve`] method unless you absolutely know
357    /// better than the allocator.
358    ///
359    /// [`reserve`]: #method.reserve
360    ///
361    /// # Panics
362    ///
363    /// Panics if the new capacity overflows `usize`.
364    ///
365    /// # Examples
366    ///
367    /// Basic usage:
368    ///
369    /// ```
370    /// use string2::String2;
371    ///
372    /// let mut s = String2::new();
373    ///
374    /// s.reserve_exact(10);
375    ///
376    /// assert!(s.capacity() >= 10);
377    /// ```
378    ///
379    /// This may not actually increase the capacity:
380    ///
381    /// ```
382    /// use string2::String2;
383    ///
384    /// let mut s = String2::with_capacity(10);
385    /// s.push('a');
386    /// s.push('b');
387    ///
388    /// // s now has a length of 2 and a capacity of 10
389    /// assert_eq!(2, s.len());
390    /// assert_eq!(10, s.capacity());
391    ///
392    /// // Since we already have an extra 8 capacity, calling this...
393    /// s.reserve_exact(8);
394    ///
395    /// // ... doesn't actually increase.
396    /// assert_eq!(10, s.capacity());
397    /// ```
398    #[inline]
399    pub fn reserve_exact(&mut self, additional: usize) {
400        self.inner.reserve_exact(additional);
401    }
402
403    /// Shrinks the capacity of this `String2` to match its length.
404    ///
405    /// # Examples
406    ///
407    /// Basic usage:
408    ///
409    /// ```
410    /// use string2::String2;
411    ///
412    /// let mut s = String2::from("foo");
413    ///
414    /// s.reserve(100);
415    /// assert!(s.capacity() >= 100);
416    ///
417    /// s.shrink_to_fit();
418    /// assert_eq!(3, s.capacity());
419    /// ```
420    #[inline]
421    pub fn shrink_to_fit(&mut self) {
422        self.inner.shrink_to_fit();
423    }
424
425    /// Converts a `String2` to a raw pointer.
426    /// As `String2` are a vector of chars, the raw pointer points to a char.
427    /// This pointer will be pointing to the first byte of the `String2`.
428    ///
429    /// # Examples
430    ///
431    /// Basic usage:
432    ///
433    /// ```
434    /// use string2::String2;
435    ///
436    /// let s = String2::from("Hello");
437    /// let ptr = s.as_ptr();
438    /// ```
439    #[inline]
440    pub fn as_ptr(&self) -> *const char {
441        self.inner.as_ptr()
442    }
443
444    /// Creates a new `String2` from a length, capacity, and pointer.
445    ///
446    /// # Safety
447    ///
448    /// This is highly unsafe, due to the number of invariants that aren't
449    /// checked:
450    ///
451    /// * The memory at `ptr` needs to have been previously allocated by the
452    ///   same allocator the standard library uses.
453    /// * `length` needs to be less than or equal to `capacity`.
454    /// * `capacity` needs to be the correct value.
455    ///
456    /// Violating these may cause problems like corrupting the allocator's
457    /// internal datastructures.
458    ///
459    /// The ownership of `ptr` is effectively transferred to the
460    /// `String2` which may then deallocate, reallocate or change the
461    /// contents of memory pointed to by the pointer at will. Ensure
462    /// that nothing else uses the pointer after calling this
463    /// function.
464    ///
465    /// # Examples
466    ///
467    /// Basic usage:
468    ///
469    /// ```
470    /// use std::mem;
471    /// use string2::String2;
472    ///
473    /// let s = String2::from("hello");
474    /// let ptr = s.as_ptr();
475    /// let len = s.len();
476    /// let capacity = s.capacity();
477    ///
478    /// mem::forget(s);
479    ///
480    /// let s = unsafe { String2::from_raw_parts(ptr as *mut _, len, capacity) };
481    ///
482    /// assert_eq!(String2::from("hello"), s);
483    /// ```
484    #[inline]
485    pub unsafe fn from_raw_parts(buf: *mut char, length: usize, capacity: usize) -> String2 {
486        String2 {
487            inner: Vec::from_raw_parts(buf, length, capacity)
488        }
489    }
490
491    /// Converts a `String2` into a byte vector.
492    ///
493    /// # Examples
494    ///
495    /// Basic usage:
496    ///
497    /// ```
498    /// use string2::String2;
499    ///
500    /// let s = String2::from("hello");
501    /// let bytes = s.as_bytes();
502    ///
503    /// assert_eq!(&[104, 101, 108, 108, 111], &bytes[..]);
504    /// ```
505    #[inline]
506    pub fn as_bytes(&self) -> Vec<u8> {
507        let s: String = self.clone().into();
508        s.into_bytes()
509    }
510
511    /// Converts a `String2` into a char slice.
512    ///
513    /// This consumes the `String2`, so we do not need to copy its contents.
514    ///
515    /// # Examples
516    ///
517    /// Basic usage:
518    ///
519    /// ```
520    /// use string2::String2;
521    ///
522    /// let s = String2::from("hello");
523    /// let bytes = s.as_slice();
524    ///
525    /// assert_eq!(&['h', 'e', 'l', 'l', 'o'][..], &bytes[..]);
526    /// ```
527    #[inline]
528    pub fn as_slice(&self) -> &[char] {
529        self.inner.as_slice()
530    }
531
532    /// Converts a `String2` into a mut char slice.
533    ///
534    /// This consumes the `String2`, so we do not need to copy its contents.
535    ///
536    /// # Examples
537    ///
538    /// Basic usage:
539    ///
540    /// ```
541    /// use string2::String2;
542    ///
543    /// let mut s = String2::from("hello");
544    /// {
545    ///     let bytes = s.as_mut_slice();
546    ///     bytes[1] = 'a';
547    /// }
548    ///
549    /// assert_eq!(String2::from("hallo"), s);
550    /// ```
551    #[inline]
552    pub fn as_mut_slice(&mut self) -> &mut [char] {
553        self.inner.as_mut_slice()
554    }
555
556    /// Converts a `String2` into a char vector.
557    ///
558    /// This consumes the `String2`, so we do not need to copy its contents.
559    ///
560    /// # Examples
561    ///
562    /// Basic usage:
563    ///
564    /// ```
565    /// use string2::String2;
566    ///
567    /// let s = String2::from("hello");
568    /// let bytes = s.as_vec();
569    ///
570    /// assert_eq!(&['h', 'e', 'l', 'l', 'o'], &bytes[..]);
571    /// ```
572    #[inline]
573    pub fn as_vec(self) -> Vec<char> {
574        self.inner
575    }
576
577    /// Converts a `String2` into a mut char slice.
578    ///
579    /// This consumes the `String2`, so we do not need to copy its contents.
580    ///
581    /// # Examples
582    ///
583    /// Basic usage:
584    ///
585    /// ```
586    /// use string2::String2;
587    ///
588    /// let mut s = String2::from("hello");
589    /// {
590    ///     let bytes = s.as_mut_vec();
591    ///     bytes[1] = 'a';
592    /// }
593    ///
594    /// assert_eq!(String2::from("hallo"), s);
595    /// ```
596    #[inline]
597    pub fn as_mut_vec(&mut self) -> &mut Vec<char> {
598        &mut self.inner
599    }
600
601    #[inline]
602    pub fn retain<F>(&mut self, f: F)
603        where F: FnMut(&char) -> bool
604    {
605        self.inner.retain(f)
606    }
607
608    #[inline]
609    pub fn get(&self, idx: usize) -> Option<&char> {
610        self.inner.get(idx)
611    }
612
613    #[inline]
614    pub fn get_mut(&mut self, idx: usize) -> Option<&mut char> {
615        self.inner.get_mut(idx)
616    }
617
618    #[inline]
619    pub fn truncate(&mut self, new_len: usize) {
620        self.inner.truncate(new_len);
621    }
622
623    #[inline]
624    pub fn push(&mut self, ch: char) {
625        self.inner.push(ch);
626    }
627
628    #[inline]
629    pub fn push_str(&mut self, string: &str) {
630        self.inner.extend(string.chars())
631    }
632
633    #[inline]
634    pub fn pop(&mut self) -> Option<char> {
635        self.inner.pop()
636    }
637
638    #[inline]
639    pub fn remove(&mut self, idx: usize) -> char {
640        self.inner.remove(idx)
641    }
642
643    #[inline]
644    pub fn insert(&mut self, idx: usize, ch: char) {
645        self.inner.insert(idx, ch);
646    }
647
648    #[inline]
649    pub fn insert_str(&mut self, _idx: usize, _string: &str) {
650        
651    }
652
653    #[inline]
654    pub fn append(&mut self, other: &mut Self) {
655        self.inner.append(&mut other.inner)
656    }
657
658    #[inline]
659    pub fn len(&self) -> usize {
660        self.inner.len()
661    }
662
663    #[inline]
664    pub fn is_empty(&self) -> bool {
665        self.inner.is_empty()
666    }
667
668    #[inline]
669    pub fn split_off(&mut self, at: usize) -> String2 {
670        let other = self.inner.split_off(at);
671
672        String2 {
673            inner: other
674        }
675    }
676
677    #[inline]
678    pub fn split_at(&self, mid: usize) -> (String2, String2) {
679        let (a, b) = self.inner.split_at(mid);
680
681        (String2 { inner: a.to_vec() }, String2 { inner: b.to_vec() })
682    }
683
684    #[inline]
685    pub fn clear(&mut self) {
686        self.inner.clear()
687    }
688
689    #[inline]
690    pub fn iter(self) -> StrIterator {
691        self.into_iter()
692    }
693}
694
695impl<'a> From<&'a str> for String2 {
696    #[inline]
697    fn from(string: &'a str) -> String2 {
698        String2 {
699            inner: string.chars().collect()
700        }
701    }
702}
703
704impl From<String> for String2 {
705    #[inline]
706    fn from(string: String) -> String2 {
707        String2 {
708            inner: string.chars().collect()
709        }
710    }
711}
712
713impl From<Vec<char>> for String2 {
714    #[inline]
715    fn from(s: Vec<char>) -> String2 {
716        String2 {
717            inner: s
718        }
719    }
720}
721
722impl<'a> From<&'a [char]> for String2 {
723    #[inline]
724    fn from(s: &'a [char]) -> String2 {
725        String2 {
726            inner: s.to_vec()
727        }
728    }
729}
730
731impl<'a> From<&'a mut [char]> for String2 {
732    #[inline]
733    fn from(s: &'a mut [char]) -> String2 {
734        String2 {
735            inner: s.to_vec()
736        }
737    }
738}
739
740impl Into<String> for String2 {
741    fn into(self) -> String {
742        self.inner.iter().map(|c| c.encode_utf8(&mut [0; 4]).to_string()).collect()
743    }
744}
745
746impl<'a> Into<String> for &'a String2 {
747    fn into(self) -> String {
748        self.inner.iter().map(|c| c.encode_utf8(&mut [0; 4]).to_string()).collect()
749    }
750}
751
752impl Default for String2 {
753    #[inline]
754    fn default() -> String2 {
755        String2::new()
756    }
757}
758
759impl IntoIterator for String2 {
760    type Item = char;
761    type IntoIter = StrIterator;
762    #[inline]
763    fn into_iter(self) -> Self::IntoIter {
764        StrIterator {
765            inner: self.inner.into_iter()
766        }
767    }
768}
769
770pub struct StrIterator {
771    inner: ::std::vec::IntoIter<char>
772}
773
774impl Iterator for StrIterator {
775    type Item = char;
776    #[inline]
777    fn next(&mut self) -> Option<char> {
778        self.inner.next()
779    }
780}
781
782impl AsRef<String2> for String2 {
783    #[inline]
784    fn as_ref(&self) -> &String2 {
785        self
786    }
787}
788
789impl AsMut<String2> for String2 {
790    #[inline]
791    fn as_mut(&mut self) -> &mut String2 {
792        self
793    }
794}
795
796impl AsRef<[char]> for String2 {
797    #[inline]
798    fn as_ref(&self) -> &[char] {
799        &self.inner
800    }
801}
802
803impl AsMut<[char]> for String2 {
804    #[inline]
805    fn as_mut(&mut self) -> &mut [char] {
806        &mut self.inner
807    }
808}
809
810impl ops::Add for String2 {
811    type Output = String2;
812    #[inline]
813    fn add(self, other: String2) -> String2 {
814        let mut self2 = self;
815        let mut other = other;
816        self2.inner.append(&mut other.inner);
817        self2
818    }
819}
820
821impl ops::Add<char> for String2 {
822    type Output = String2;
823    #[inline]
824    fn add(mut self, other: char) -> String2 {
825        self.push(other);
826        self
827    }
828}
829
830impl<'a> ops::Add<&'a str> for String2 {
831    type Output = String2;
832    #[inline]
833    fn add(mut self, other: &str) -> String2 {
834        self.push_str(other);
835        self
836    }
837}
838
839impl ops::AddAssign for String2 {
840    #[inline]
841    fn add_assign(&mut self, other: String2) {
842        let mut other = other;
843        self.inner.append(other.inner.as_mut())
844    }
845}
846
847impl ops::AddAssign<char> for String2 {
848    #[inline]
849    fn add_assign(&mut self, other: char) {
850        self.push(other)
851    }
852}
853
854impl<'a> ops::AddAssign<&'a str> for String2 {
855    #[inline]
856    fn add_assign(&mut self, other: &str) {
857        self.push_str(other)
858    }
859}
860
861impl PartialEq for String2 {
862    #[inline]
863    fn eq(&self, other: &String2) -> bool {
864        self.inner == other.inner
865    }
866}
867
868impl PartialOrd for String2 {
869    #[inline]
870    fn partial_cmp(&self, other: &String2) -> Option<::std::cmp::Ordering> {
871        PartialOrd::partial_cmp(&self.inner, &other.inner)
872    }
873}
874
875impl ops::Index<usize> for String2 {
876    type Output = char;
877    #[inline]
878    fn index(&self, idx: usize) -> &char {
879        &self.inner[idx]
880    }
881}
882
883impl ops::Index<ops::Range<usize>> for String2 {
884    type Output = [char];
885    #[inline]
886    fn index(&self, range: ops::Range<usize>) -> &[char] {
887        self.inner.index(range)
888    }
889}
890
891impl ops::Index<ops::RangeFrom<usize>> for String2 {
892    type Output = [char];
893    #[inline]
894    fn index(&self, range: ops::RangeFrom<usize>) -> &[char] {
895        self.inner.index(range)
896    }
897}
898
899impl ops::Index<ops::RangeTo<usize>> for String2 {
900    type Output = [char];
901    #[inline]
902    fn index(&self, range: ops::RangeTo<usize>) -> &[char] {
903        self.inner.index(range)
904    }
905}
906
907impl ops::Index<ops::RangeFull> for String2 {
908    type Output = [char];
909    #[inline]
910    fn index(&self, _range: ops::RangeFull) -> &[char] {
911        self.as_ref()
912    }
913}
914
915impl ops::IndexMut<usize> for String2 {
916    #[inline]
917    fn index_mut(&mut self, idx: usize) -> &mut char {
918        &mut self.inner[idx]
919    }
920}
921
922impl ops::IndexMut<ops::Range<usize>> for String2 {
923    #[inline]
924    fn index_mut(&mut self, range: ops::Range<usize>) -> &mut [char] {
925        self.inner.index_mut(range)
926    }
927}
928
929impl ops::IndexMut<ops::RangeFrom<usize>> for String2 {
930    #[inline]
931    fn index_mut(&mut self, range: ops::RangeFrom<usize>) -> &mut [char] {
932        self.inner.index_mut(range)
933    }
934}
935
936impl ops::IndexMut<ops::RangeTo<usize>> for String2 {
937    #[inline]
938    fn index_mut(&mut self, range: ops::RangeTo<usize>) -> &mut [char] {
939        self.inner.index_mut(range)
940    }
941}
942
943impl ops::IndexMut<ops::RangeFull> for String2 {
944    #[inline]
945    fn index_mut(&mut self, range: ops::RangeFull) -> &mut [char] {
946        self.inner.index_mut(range)
947    }
948}
949
950impl fmt::Display for String2 {
951    #[inline]
952    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
953        let s: String = self.into();
954        fmt::Display::fmt(&s, f)
955    }
956}
957
958impl fmt::Debug for String2 {
959    #[inline]
960    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
961        let s: String = self.into();
962        fmt::Debug::fmt(&s, f)
963    }
964}