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
use std::{alloc::Allocator, borrow::Borrow, cmp::Ordering};

use generic_vec::{
    raw::{Storage, StorageWithCapacity},
    GenericVec, HeapVec,
};

#[derive(Default, Copy, Clone)]
#[repr(transparent)]
pub struct StringBase<S: ?Sized> {
    pub(crate) storage: S,
}

impl<S: StorageWithCapacity<T>, T> StringBase<GenericVec<T, S>> {
    #[inline]
    pub fn new_with_capacity(capacity: usize) -> Self {
        Self::with_storage(S::with_capacity(capacity))
    }
}

pub type OwnedString<U, S> = StringBase<GenericVec<U, S>>;
pub type StringSlice<U> = StringBase<[U]>;

impl<S: ?Sized + Storage<T>, T> StringBase<GenericVec<T, S>> {
    /// Creates a new empty `String` with a particular storage backend.
    ///
    /// `String`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 `String`, 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 `String`, reducing the number of
    /// reallocations it needs to do.
    ///
    /// [`capacity`]: StringBase::capacity
    ///
    /// If the given capacity is `0`, no allocation will occur, and this method
    /// is identical to the [`new`] method.
    ///
    /// [`new`]: StringBase::new
    ///
    /// # Examples
    ///
    /// Basic usage:
    ///
    /// ```
    /// # use generic_str::String;
    /// let mut s = String::with_capacity(10);
    ///
    /// // The String 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 _ in 0..10 {
    ///     s.push('a');
    /// }
    ///
    /// assert_eq!(s.capacity(), cap);
    ///
    /// // ...but this may make the string reallocate
    /// s.push('a');
    /// ```
    #[inline]
    pub const fn with_storage(storage: S) -> Self
    where
        S: Sized,
    {
        StringBase {
            storage: GenericVec::with_storage(storage),
        }
    }
}

impl<T: ?Sized + std::ops::Deref> std::ops::Deref for StringBase<T> {
    type Target = StringBase<T::Target>;

    fn deref(&self) -> &StringBase<T::Target> {
        unsafe { std::mem::transmute::<&T::Target, &StringBase<T::Target>>(self.storage.deref()) }
    }
}

impl<T: ?Sized + std::ops::DerefMut> std::ops::DerefMut for StringBase<T> {
    fn deref_mut(&mut self) -> &mut StringBase<T::Target> {
        unsafe {
            std::mem::transmute::<&mut T::Target, &mut StringBase<T::Target>>(
                self.storage.deref_mut(),
            )
        }
    }
}

impl<T: ?Sized + AsRef<U>, U: ?Sized> AsRef<StringBase<U>> for StringBase<T> {
    fn as_ref(&self) -> &StringBase<U> {
        unsafe { std::mem::transmute::<&U, &StringBase<U>>(self.storage.as_ref()) }
    }
}

impl<T: ?Sized + AsMut<U>, U: ?Sized> AsMut<StringBase<U>> for StringBase<T> {
    fn as_mut(&mut self) -> &mut StringBase<U> {
        unsafe { std::mem::transmute::<&mut U, &mut StringBase<U>>(self.storage.as_mut()) }
    }
}

impl<T, A: Allocator> Borrow<StringBase<[T]>> for StringBase<HeapVec<T, A>> {
    fn borrow(&self) -> &StringBase<[T]> {
        unsafe { std::mem::transmute::<&[T], &StringBase<[T]>>(self.storage.borrow()) }
    }
}

impl<T: Clone> ToOwned for StringBase<[T]> {
    type Owned = StringBase<HeapVec<T>>;

    fn to_owned(&self) -> Self::Owned {
        Self::Owned {
            storage: self.storage.to_owned().into(),
        }
    }
}

impl<S: ?Sized + Storage<U>, T: ?Sized + Storage<U>, U> PartialEq<OwnedString<U, T>>
    for StringBase<GenericVec<U, S>>
where
    GenericVec<U, S>: PartialEq<GenericVec<U, T>>,
{
    fn eq(&self, other: &OwnedString<U, T>) -> bool {
        self.storage.eq(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialEq> PartialEq<OwnedString<U, S>> for StringSlice<U> {
    fn eq(&self, other: &OwnedString<U, S>) -> bool {
        other.storage.eq(&self.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialEq> PartialEq<OwnedString<U, S>> for &StringSlice<U> {
    fn eq(&self, other: &OwnedString<U, S>) -> bool {
        other.storage.eq(&self.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialEq> PartialEq<StringSlice<U>> for OwnedString<U, S> {
    fn eq(&self, other: &StringSlice<U>) -> bool {
        self.storage.eq(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialEq> PartialEq<&StringSlice<U>> for OwnedString<U, S> {
    fn eq(&self, other: &&StringSlice<U>) -> bool {
        self.storage.eq(&other.storage)
    }
}

impl<U: PartialEq> PartialEq<StringSlice<U>> for StringSlice<U> {
    fn eq(&self, other: &StringSlice<U>) -> bool {
        self.storage.eq(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: Eq> Eq for OwnedString<U, S> {}
impl<U: Eq> Eq for StringSlice<U> {}

impl<S: ?Sized + Storage<U>, T: ?Sized + Storage<U>, U> PartialOrd<OwnedString<U, T>>
    for StringBase<GenericVec<U, S>>
where
    GenericVec<U, S>: PartialOrd<GenericVec<U, T>>,
{
    fn partial_cmp(&self, other: &OwnedString<U, T>) -> Option<Ordering> {
        self.storage.partial_cmp(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialOrd> PartialOrd<OwnedString<U, S>> for StringSlice<U> {
    fn partial_cmp(&self, other: &OwnedString<U, S>) -> Option<Ordering> {
        other.storage.partial_cmp(&self.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialOrd> PartialOrd<OwnedString<U, S>> for &StringSlice<U> {
    fn partial_cmp(&self, other: &OwnedString<U, S>) -> Option<Ordering> {
        other.storage.partial_cmp(&self.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialOrd> PartialOrd<StringSlice<U>> for OwnedString<U, S> {
    fn partial_cmp(&self, other: &StringSlice<U>) -> Option<Ordering> {
        self.storage.partial_cmp(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: PartialOrd> PartialOrd<&StringSlice<U>> for OwnedString<U, S> {
    fn partial_cmp(&self, other: &&StringSlice<U>) -> Option<Ordering> {
        self.storage.partial_cmp(&other.storage)
    }
}

impl<U: PartialOrd> PartialOrd<StringSlice<U>> for StringSlice<U> {
    fn partial_cmp(&self, other: &StringSlice<U>) -> Option<Ordering> {
        self.storage.partial_cmp(&other.storage)
    }
}

impl<S: ?Sized + Storage<U>, U: Ord> Ord for OwnedString<U, S> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.storage.cmp(&other.storage)
    }
}
impl<U: Ord> Ord for StringSlice<U> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.storage.cmp(&other.storage)
    }
}