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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::borrow::{Borrow, Cow};
use std::cmp;
use std::fmt;
use std::hash;
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::slice;
use std::str;

// All bits set except the highest
const MAX_LEN: usize = !0 >> 1;

// Only the highest bit
const OWNED_TAG: usize = MAX_LEN + 1;

/// Like `Cow<'a, str>`, but with smaller `std::mem::size_of`. (Two words instead of four.)
pub struct CompactCowStr<'a> {
    // `tagged_len` is a tag in its highest bit, and the string length in the rest of the bits.
    //
    // * If the tag is 1, the memory pointed to by `ptr` is owned
    //   and the lifetime parameter is irrelevant.
    //   `ptr` and `len` are the components of a `Box<str>`.
    //
    // * If the tag is 0, the memory is borrowed.
    //   `ptr` and `len` are the components of a `&'a str`.

    // FIXME: https://github.com/rust-lang/rust/issues/27730 use NonZero or Shared
    ptr: *const u8,
    tagged_len: usize,
    phantom: PhantomData<&'a str>,
}

impl<'a> From<&'a str> for CompactCowStr<'a> {
    #[inline]
    fn from(s: &'a str) -> Self {
        let len = s.len();
        assert!(len <= MAX_LEN);
        CompactCowStr {
            ptr: s.as_ptr(),
            tagged_len: len,
            phantom: PhantomData,
        }
    }
}

impl<'a> From<Box<str>> for CompactCowStr<'a> {
    #[inline]
    fn from(s: Box<str>) -> Self {
        let ptr = s.as_ptr();
        let len = s.len();
        assert!(len <= MAX_LEN);
        mem::forget(s);
        CompactCowStr {
            ptr: ptr,
            tagged_len: len | OWNED_TAG,
            phantom: PhantomData,
        }
    }
}

impl<'a> CompactCowStr<'a> {
    /// Whether this string refers to borrowed memory
    /// (as opposed to owned, which would be freed when `CompactCowStr` goes out of scope).
    #[inline]
    pub fn is_borrowed(&self) -> bool {
        (self.tagged_len & OWNED_TAG) == 0
    }

    /// The length of this string
    #[inline]
    pub fn len(&self) -> usize {
        self.tagged_len & !OWNED_TAG
    }

    // Intentionally private since it is easy to use incorrectly.
    #[inline]
    fn as_raw_str(&self) -> *const str {
        unsafe {
            str::from_utf8_unchecked(slice::from_raw_parts(self.ptr, self.len()))
        }
    }

    /// If this string is borrowed, return a slice with the original lifetime,
    /// not borrowing `self`.
    ///
    /// (`Deref` is implemented unconditionally, but returns a slice with a shorter lifetime.)
    #[inline]
    pub fn as_str(&self) -> Option<&'a str> {
        if self.is_borrowed() {
            Some(unsafe { &*self.as_raw_str() })
        } else {
            None
        }
    }

    /// Convert into `String`, re-using the memory allocation if it was already owned.
    #[inline]
    pub fn into_owned(self) -> String {
        unsafe {
            let raw = self.as_raw_str();
            let is_borrowed = self.is_borrowed();
            mem::forget(self);
            if is_borrowed {
                String::from(&*raw)
            } else {
                Box::from_raw(raw as *mut str).into_string()
            }
        }
    }
}

impl<'a> Clone for CompactCowStr<'a> {
    #[inline]
    fn clone(&self) -> Self {
        if self.is_borrowed() {
            CompactCowStr { ..*self }
        } else {
            Self::from(String::from(&**self).into_boxed_str())
        }
    }
}

impl<'a> Drop for CompactCowStr<'a> {
    #[inline]
    fn drop(&mut self) {
        if !self.is_borrowed() {
            unsafe {
                Box::from_raw(self.as_raw_str() as *mut str);
            }
        }
    }
}

impl<'a> Deref for CompactCowStr<'a> {
    type Target = str;

    #[inline]
    fn deref(&self) -> &str {
        unsafe {
            &*self.as_raw_str()
        }
    }
}

impl<'a> From<CompactCowStr<'a>> for Cow<'a, str> {
    #[inline]
    fn from(cow: CompactCowStr<'a>) -> Self {
        unsafe {
            let raw = cow.as_raw_str();
            let is_borrowed = cow.is_borrowed();
            mem::forget(cow);
            if is_borrowed {
                Cow::Borrowed(&*raw)
            } else {
                Cow::Owned(Box::from_raw(raw as *mut str).into_string())
            }
        }
    }
}

impl<'a> From<String> for CompactCowStr<'a> {
    #[inline]
    fn from(s: String) -> Self {
        Self::from(s.into_boxed_str())
    }
}

impl<'a> From<Cow<'a, str>> for CompactCowStr<'a> {
    #[inline]
    fn from(s: Cow<'a, str>) -> Self {
        match s {
            Cow::Borrowed(s) => Self::from(s),
            Cow::Owned(s) => Self::from(s),
        }
    }
}

impl<'a> AsRef<str> for CompactCowStr<'a> {
    #[inline]
    fn as_ref(&self) -> &str {
        self
    }
}

impl<'a> Borrow<str> for CompactCowStr<'a> {
    #[inline]
    fn borrow(&self) -> &str {
        self
    }
}

impl<'a> Default for CompactCowStr<'a> {
    #[inline]
    fn default() -> Self {
        Self::from("")
    }
}

impl<'a> hash::Hash for CompactCowStr<'a> {
    #[inline]
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        str::hash(self, hasher)
    }
}

impl<'a, T: AsRef<str>> PartialEq<T> for CompactCowStr<'a> {
    #[inline]
    fn eq(&self, other: &T) -> bool {
        str::eq(self, other.as_ref())
    }
}

impl<'a, T: AsRef<str>> PartialOrd<T> for CompactCowStr<'a> {
    #[inline]
    fn partial_cmp(&self, other: &T) -> Option<cmp::Ordering> {
        str::partial_cmp(self, other.as_ref())
    }
}

impl<'a> Eq for CompactCowStr<'a> {}

impl<'a> Ord for CompactCowStr<'a> {
    #[inline]
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        str::cmp(self, other)
    }
}

impl<'a> fmt::Display for CompactCowStr<'a> {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        str::fmt(self, formatter)
    }
}

impl<'a> fmt::Debug for CompactCowStr<'a> {
    #[inline]
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        str::fmt(self, formatter)
    }
}