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
// Copyright (c) 2021 Sebastien Braun
//
// 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/.

//! The [`Take`] and [`Borrow`] traits abstract over the duality
//! between owned and borrowed types, much in the same way as the
//! standard-library [`Borrow`](std::borrow::Borrow) and
//! [`ToOwned`](std::borrow::ToOwned) traits do.
//!
//! The difference between these and the standard-library traits is
//! that the traits here are more generic. See the specific traits
//! for details.
//!
//! `Eso` differentiates three different categories of values:
//!
//! |       | Category  | Analogy            | Description
//! |------:|-----------|--------------------|----------------------
//! | **E** | Ephemeral | `&'a `[`str`]      | A reference to a shared value with a limited lifetime
//! | **S** | Static    | `&'static `[`str`] | A reference to a shared value that client code can hold on to indefinitely
//! | **O** | Owned     | [`String`]         | A value that is exclusively owned and can be mutated (if no references to it have been borrowed out)
//!
//! These characteristics are enforced by [`Eso`](crate::eso::Eso)
//! only on a per-function basis, but not in general. Client code should
//! enforce them where necessary.
//!
//! This module defines traits to convert between the categories:
//!
//! | From      | To Ephemeral | To Static                                                  | To Owned
//! |-----------|--------------|------------------------------------------------------------|----------
//! | Ephemeral |              | [`TryInternRef`], [`InternRef`]                            | [`Take`]
//! | Static    | [`Borrow`]   |                                                            | [`Take`]
//! | Owned     | [`Borrow`]   | [`TryInternRef`], [`TryIntern`], [`InternRef`], [`Intern`] |
//!
//! As can be seen from the table, there is some additional complexity
//! regarding the interning operation:
//!
//!  1. **Interning may fail:** Depending on the implementation, not all
//!     values may have a static counterpart.
//!  2. **Owned values may offer optimization opportunities:** If the
//!     owned value is not needed after the interning operation, it is
//!     cheaper to move it into the interning function.
//!
//! ## Open questions / TODO
//!
//!  - [ ] actually implement the `...Intern...` traits
//!  - [ ] think about naming:
//!    - `Borrow` clashes with `std`
//!    - `Take` does not seem like a good description of what is actually
//!      happening
//!  - [ ] is `Borrow`ing from an Owned really the same operation as
//!    `Borrow`ing from a static reference?
use std::{
    borrow::Cow,
    ffi::{CStr, CString, OsStr, OsString},
    path::{Path, PathBuf},
    rc::Rc,
    sync::Arc,
};

/// A value that can be borrowed as a generalized reference of type `T`.
///
/// ```
/// # use eso::borrow::Borrow;
/// let value = String::from("Hello World");
/// let reference: &str = value.borrow();
/// ```
///
/// The difference to [`Borrow`](std::borrow::Borrow) is that
/// this trait allows you to return types that are not actually references,
/// such as [`Cow`]s:
///
/// ```
/// # use eso::borrow::Borrow; use std::borrow::Cow;
/// let value = String::from("Hello World");
/// let reference: Cow<str> = value.borrow();
/// ```
pub trait Borrow<'a, T: 'a> {
    /// Borrow a generalized reference of type `T`.
    fn borrow(&'a self) -> T;
}

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

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

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

impl<'a, T> Borrow<'a, &'a T> for T {
    fn borrow(&'a self) -> &'a T {
        self
    }
}

impl<'a, T, R> Borrow<'a, Cow<'a, R>> for T
where
    T: std::borrow::Borrow<R>,
    R: ?Sized + ToOwned<Owned = T>,
{
    fn borrow(&'a self) -> Cow<R> {
        Cow::Borrowed(self.borrow())
    }
}

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

impl<'a> Borrow<'a, &'a [u8]> for String {
    #[inline]
    fn borrow(&'a self) -> &'a [u8] {
        self.as_bytes()
    }
}

impl<'a> Borrow<'a, &'a Path> for PathBuf {
    #[inline]
    fn borrow(&self) -> &Path {
        self.as_path()
    }
}

impl<'a, T> Borrow<'a, &'a [T]> for Vec<T> {
    #[inline]
    fn borrow(&self) -> &[T] {
        self.as_slice()
    }
}

impl<'a> Borrow<'a, &'a OsStr> for OsString {
    #[inline]
    fn borrow(&self) -> &OsStr {
        self.as_os_str()
    }
}

impl<'a> Borrow<'a, &'a OsStr> for PathBuf {
    #[inline]
    fn borrow(&self) -> &OsStr {
        self.as_os_str()
    }
}

impl<'a> Borrow<'a, &'a CStr> for CString {
    #[inline]
    fn borrow(&self) -> &CStr {
        self.as_c_str()
    }
}

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

#[cfg(unix)]
mod unix {
    use super::*;
    use std::os::unix::ffi::OsStrExt;

    impl<'a> Borrow<'a, &'a [u8]> for OsString {
        #[inline]
        fn borrow(&self) -> &[u8] {
            self.as_os_str().as_bytes()
        }
    }

    impl<'a> Borrow<'a, &'a [u8]> for PathBuf {
        #[inline]
        fn borrow(&self) -> &[u8] {
            self.as_os_str().as_bytes()
        }
    }

    impl<'a> Take<OsString> for &'a [u8] {
        fn to_owned(&self) -> OsString {
            OsStr::from_bytes(self).to_os_string()
        }
    }

    impl<'a> Take<PathBuf> for &'a [u8] {
        fn to_owned(&self) -> PathBuf {
            PathBuf::from(OsStr::from_bytes(self))
        }
    }
}

/// A version of the [`ToOwned`] trait describing *generalized* references
/// from which an owned form can be cloned.
///
/// The obvious instances for [`str`], [`Path`], [`OsStr`], [`CStr`],
/// slices as well as sized types implementing [`Clone`] are implemented
/// out of the box.
pub trait Take<O>: Sized {
    /// Clone the thing denoted by a generalized reference into one that
    /// is owned.
    ///
    /// This trait function defaults to calling [`to_owned`](Take::to_owned)
    /// but is there as an optimization opportunity, if needed.
    fn own(self) -> O {
        self.to_owned()
    }

    /// Clone the thing denoted by a generalized reference into one that
    /// is owned without consuming the reference.
    fn to_owned(&self) -> O;
}

impl<'a> Take<String> for &'a str {
    fn to_owned(&self) -> String {
        self.to_string()
    }
}

impl<'a> Take<PathBuf> for &'a Path {
    fn to_owned(&self) -> PathBuf {
        self.to_path_buf()
    }
}

impl<'a, T: Clone> Take<Vec<T>> for &'a [T] {
    fn to_owned(&self) -> Vec<T> {
        self.to_vec()
    }
}

impl<'a> Take<OsString> for &'a OsStr {
    fn to_owned(&self) -> OsString {
        self.to_os_string()
    }
}

impl<'a> Take<PathBuf> for &'a OsStr {
    fn to_owned(&self) -> PathBuf {
        PathBuf::from(self)
    }
}

impl<'a, T: Clone> Take<T> for &'a T {
    fn to_owned(&self) -> T {
        (*self).clone()
    }
}

impl<'a> Take<CString> for &'a CStr {
    fn to_owned(&self) -> CString {
        (*self).to_owned()
    }
}

impl<'a, T: Clone> Take<Box<T>> for &'a T {
    fn to_owned(&self) -> Box<T> {
        Box::new((*self).clone())
    }
}

impl<'a, T: Clone> Take<Rc<T>> for &'a T {
    fn to_owned(&self) -> Rc<T> {
        Rc::new((*self).clone())
    }
}

impl<'a, T: Clone> Take<Arc<T>> for &'a T {
    fn to_owned(&self) -> Arc<T> {
        Arc::new((*self).clone())
    }
}

impl<'a, R: ToOwned> Take<R::Owned> for Cow<'a, R> {
    fn to_owned(&self) -> R::Owned {
        self.clone().into_owned()
    }
}

macro_rules!forward_trait{
    (@impl@ $to:ty, $fn:ident ( $($decl:tt)* ) -> $out:ty => $wrap:ident($ftrait:ident::$ffn:ident($($use:tt)*)) ) => {
        fn $fn( $($decl)* ) -> $out {
            $wrap(<Self as $ftrait<$to>>::$ffn( $($use)* ))
        }
    };
    (@fn@ TryInternRef $to:ty) => {
        forward_trait!(@impl@ $to, try_intern_ref(&self) -> Option<$to> => Some(InternRef::intern_ref(self)));
    };
    (@fn@ TryIntern $to:ty) => {
        forward_trait!(@impl@ $to, try_intern(self) -> Result<$to, Self> => Ok(Intern::intern(self)));
    };
    (
        $(<
            $($g:tt)+
        >)?
        $trait:ident,
        $to:ty,
        $from:ty
    ) => {
        impl $(<$($g)+>)? $trait<$to> for $from {
            forward_trait!(@fn@ $trait $to);
        }
    };
}

/// A value that can be interned from a reference,
/// where interning may fail.
pub trait TryInternRef<T> {
    /// Look up or create a static reference for the
    /// value denoted by `self`.
    /// Return `None` if it is not possible to represent
    /// the value denoted by `self` as a static reference.
    fn try_intern_ref(&self) -> Option<T>;
}

forward_trait!(TryInternRef, Rc<str>, &'_ str);
forward_trait!(TryInternRef, Arc<str>, &'_ str);

/// A value that can be interned from a reference,
/// where interning cannot fail.
pub trait InternRef<T>: TryInternRef<T> {
    /// Look up or create a static reference for the
    /// value denoted by `self`.
    fn intern_ref(&self) -> T;
}

impl InternRef<Rc<str>> for &'_ str {
    fn intern_ref(&self) -> Rc<str> {
        Rc::from(*self)
    }
}

impl InternRef<Arc<str>> for &'_ str {
    fn intern_ref(&self) -> Arc<str> {
        Arc::from(*self)
    }
}

/// A value that can be interned from an owned value,
/// where interning may fail.
pub trait TryIntern<T>: Sized {
    /// Look up or create a static reference to the value of `self`.
    /// Return `None` if it is not possible to represent
    /// the value denoted by `self` as a static reference.
    fn try_intern(self) -> Result<T, Self>;
}

forward_trait!(TryIntern, Rc<str>, String);
forward_trait!(TryIntern, Arc<str>, String);

/// A value that can be interned from an owned value,
/// where interning cannot fail.
pub trait Intern<T> {
    /// Look up or create a static reference to the value of `self`.
    fn intern(self) -> T;
}

impl Intern<Rc<str>> for String {
    fn intern(self) -> Rc<str> {
        Rc::from(self)
    }
}

impl Intern<Arc<str>> for String {
    fn intern(self) -> Arc<str> {
        Arc::from(self)
    }
}