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
// Copyright 2018 Eduardo Sánchez Muñoz
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

//! This crate provides `Rob<'a, T>` a type that can contain either a
//! borrwed reference or an owned `Box`. It is similar to `std::borrow::Cow<'a, T>`,
//! but it always uses a `Box` to stored owned values.
//!
//! The main difference with `Cow` is that `Rob` is not implemented as
//! an enum, instead it is a struct with a pointer and a flag that
//! indicates whether the value is owned or not. This allows to use
//! the value by accessing directly the pointer, without the overhead
//! of matching an enum needed by `Cow`.

#[cfg(test)]
mod tests;

use std::ptr::NonNull;
use std::marker::PhantomData;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};

/// The `Rob` type. See the crate documentation.
pub struct Rob<'a, T: 'a + ?Sized> {
    ptr: NonNull<T>,
    is_owned: bool,
    marker1: PhantomData<&'a T>,
    marker2: PhantomData<T>,
}

impl<'a, T: 'a + ?Sized> Drop for Rob<'a, T> {
    fn drop(&mut self) {
        if self.is_owned {
            unsafe { Box::from_raw(self.ptr.as_ptr()) };
        }
    }
}

impl<'a, T: 'a> Rob<'a, T> {
    /// Creates a new `Rob` with an owned value.
    ///
    /// Example
    /// -------
    /// ```
    /// let x = rob::Rob::from_value(123i32);
    /// assert_eq!(*x, 123);
    /// assert!(rob::Rob::is_owned(&x));
    /// ```
    #[inline]
    pub fn from_value(value: T) -> Self {
        Self::from_box(Box::new(value))
    }
}

impl<'a, T: 'a + ?Sized> Rob<'a, T> {
    /// Creates a new `Rob` with a borrowed reference.
    ///
    /// Example
    /// -------
    /// ```
    /// let value = 123i32;
    /// let x = rob::Rob::from_ref(&value);
    /// assert_eq!(*x, 123);
    /// assert!(!rob::Rob::is_owned(&x));
    /// ```
    #[inline]
    pub fn from_ref(r: &'a T) -> Self {
        Self {
            ptr: NonNull::from(r),
            is_owned: false,
            marker1: PhantomData,
            marker2: PhantomData,
        }
    }
    
    /// Creates a new `Rob` with an owned value that is already boxed.
    ///
    /// Example
    /// -------
    /// ```
    /// let x = rob::Rob::from_box(Box::new(123i32));
    /// assert_eq!(*x, 123);
    /// assert!(rob::Rob::is_owned(&x));
    /// ```
    #[inline]
    pub fn from_box(b: Box<T>) -> Self {
        Self {
            ptr: unsafe { NonNull::new_unchecked(Box::into_raw(b)) },
            is_owned: true,
            marker1: PhantomData,
            marker2: PhantomData,
        }
    }
    
    /// Creates a new `Rob` from a raw pointer and an owned flag. If
    /// `is_owned` is `true`, `ptr` should come from `Box::into_raw`.
    #[inline]
    pub unsafe fn from_raw(ptr: *mut T, is_owned: bool) -> Self {
        Self {
            ptr: NonNull::new_unchecked(ptr),
            is_owned: is_owned,
            marker1: PhantomData,
            marker2: PhantomData,
        }
    }
    
    /// Consumes `this`, returning a raw pointer to the value and a
    /// flag indicating whether the values is owned or not.
    #[inline]
    pub fn into_raw(this: Self) -> (*mut T, bool) {
        let ptr = this.ptr.as_ptr();
        let is_owned = this.is_owned;
        std::mem::forget(this);
        (ptr, is_owned)
    }
    
    /// If the value is not owned, returns a reference to it with
    /// lifetime `'a`.
    #[inline]
    pub fn as_ref(this: &Self) -> Option<&'a T> {
        if !this.is_owned {
            unsafe { Some(&*this.ptr.as_ptr()) }
        } else {
            None
        }
    }
    
    /// Returns whether the value is owned or not.
    #[inline]
    pub fn is_owned(this: &Self) -> bool {
        this.is_owned
    }
}

impl<'a, T: 'a + ?Sized> Rob<'a, T>
    where T: std::borrow::ToOwned,
          <T as std::borrow::ToOwned>::Owned: Into<Box<T>>
{
    /// Consumes `this`, returning a `Box` containing the value, cloning
    /// it if it was not owned.
    pub fn into_box(this: Self) -> Box<T> {
        if this.is_owned {
            let ptr = this.ptr.as_ptr();
            std::mem::forget(this);
            unsafe { Box::from_raw(ptr) }
        } else {
            this.to_owned().into()
        }
    }
    
    /// Returns a mutable reference to the value, cloning it if it was
    /// not owned.
    pub fn to_mut(this: &mut Self) -> &mut T {
        unsafe {
            if !this.is_owned {
                let b: Box<T> = this.to_owned().into();
                this.ptr = NonNull::new_unchecked(Box::into_raw(b));
                this.is_owned = true;
            }
            
            &mut *this.ptr.as_mut()
        }
    }
}

impl<'a, T: 'a> From<T> for Rob<'a, T> {
    #[inline]
    fn from(value: T) -> Self {
        Self::from_value(value)
    }
}

impl<'a, T: 'a + ?Sized> From<&'a T> for Rob<'a, T> {
    #[inline]
    fn from(r: &'a T) -> Self {
        Self::from_ref(r)
    }
}

impl<'a, T: 'a + ?Sized> From<Box<T>> for Rob<'a, T> {
    #[inline]
    fn from(b: Box<T>) -> Self {
        Self::from_box(b)
    }
}

impl<'a, T: 'a> From<Vec<T>> for Rob<'a, [T]> {
    #[inline]
    fn from(vec: Vec<T>) -> Self {
        Self::from_box(vec.into_boxed_slice())
    }
}

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

impl<'a> From<std::ffi::CString> for Rob<'a, std::ffi::CStr> {
    #[inline]
    fn from(s: std::ffi::CString) -> Self {
        Self::from_box(s.into_boxed_c_str())
    }
}

impl<'a> From<std::ffi::OsString> for Rob<'a, std::ffi::OsStr> {
    #[inline]
    fn from(s: std::ffi::OsString) -> Self {
        Self::from_box(s.into_boxed_os_str())
    }
}

impl<'a> From<std::path::PathBuf> for Rob<'a, std::path::Path> {
    #[inline]
    fn from(s: std::path::PathBuf) -> Self {
        Self::from_box(s.into_boxed_path())
    }
}

impl<'a, T> From<std::borrow::Cow<'a, T>> for Rob<'a, T>
    where T: std::borrow::ToOwned,
          <T as std::borrow::ToOwned>::Owned: Into<Box<T>>,
{
    fn from(cow: std::borrow::Cow<'a, T>) -> Self {
        match cow {
            std::borrow::Cow::Borrowed(r) => Self::from_ref(r),
            std::borrow::Cow::Owned(o) => Self::from_box(o.into()),
        }
    }
}

impl<'a, T: 'a + Clone> Clone for Rob<'a, T> {
    fn clone(&self) -> Self {
        if self.is_owned {
            Self::from_value((**self).clone())
        } else {
            unsafe {
                Self::from_ref(&*self.ptr.as_ptr())
            }
        }
    }
}

impl<'a, T: 'a + ?Sized + Debug> Debug for Rob<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        <T as Debug>::fmt(&**self, f)
    }
}

impl<'a, T: 'a + ?Sized + Hash> Hash for Rob<'a, T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        <T as Hash>::hash(&**self, state)
    }
}

impl<'a, T: 'a + ?Sized + PartialEq> PartialEq for Rob<'a, T> {
    #[inline]
    fn eq(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialEq>::eq(&**self, &**other)
    }
    
    #[inline]
    fn ne(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialEq>::ne(&**self, &**other)
    }
}

impl<'a, T: 'a + ?Sized + PartialOrd> PartialOrd for Rob<'a, T> {
    #[inline]
    fn partial_cmp(&self, other: &Rob<'a, T>) -> Option<std::cmp::Ordering> {
        <T as PartialOrd>::partial_cmp(&**self, &**other)
    }
    
    #[inline]
    fn lt(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialOrd>::lt(&**self, &**other)
    }
    
    #[inline]
    fn le(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialOrd>::le(&**self, &**other)
    }
    
    #[inline]
    fn ge(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialOrd>::ge(&**self, &**other)
    }
    
    #[inline]
    fn gt(&self, other: &Rob<'a, T>) -> bool {
        <T as PartialOrd>::gt(&**self, &**other)
    }
}

impl<'a, T: 'a + ?Sized> std::ops::Deref for Rob<'a, T> {
    type Target = T;
    
    #[inline]
    fn deref(&self) -> &T {
        unsafe { &*self.ptr.as_ptr() }
    }
}

impl<'a, T: 'a + ?Sized> std::borrow::Borrow<T> for Rob<'a, T> {
    #[inline]
    fn borrow(&self) -> &T {
        &**self
    }
}

impl<'a, T: 'a + ?Sized> AsRef<T> for Rob<'a, T> {
    #[inline]
    fn as_ref(&self) -> &T {
        &**self
    }
}