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
//! Mixed references to owned and borrowed memory.
//!
//! # Usage
//!
//! This crate is available [on crates.io](https://crates.io/crates/mixed_ref)
//! and can be used by adding the following to your project's `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! mixed_ref = "0.1.1"
//! ```
//!
//! and this to your crate root:
//!
//! ```
//! #[macro_use]
//! extern crate mixed_ref;
//! # fn main() {}
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]

#[cfg(feature = "std")]
extern crate core;

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
#[cfg(not(feature = "std"))]
use alloc::{String, Vec};

use core::ops::{Deref, DerefMut};
use core::borrow::{Borrow, BorrowMut};

/// A reference to either owned or borrowed data.
///
/// If the data is owned, it's recommended to provide the `'static` lifetime.
/// In some cases, there's no other option.
///
/// If mutably borrowing data, use [`MixedRefMut`].
///
/// [`MixedRefMut`]: enum.MixedRefMut.html
#[derive(Debug, Eq, Hash)]
pub enum MixedRef<'a, T: ?Sized + 'a> {
    /// Owned, boxed data.
    Owned(Box<T>),
    /// Borrowed data.
    Borrowed(&'a T)
}

/// A reference to either owned or mutably borrowed data.
///
/// This acts similarly to [`MixedRef`], except that the inner data is mutable.
///
/// [`MixedRef`]: enum.MixedRef.html
#[derive(Debug, Eq, Hash)]
pub enum MixedRefMut<'a, T: ?Sized + 'a> {
    /// Owned, boxed data.
    Owned(Box<T>),
    /// Borrowed, mutable data.
    Borrowed(&'a mut T)
}

impl<'a, T: ?Sized + AsRef<U>, U: ?Sized + PartialEq> PartialEq<T> for MixedRef<'a, U> {
    fn eq(&self, other: &T) -> bool {
        (self as &U) == other.as_ref()
    }
}

impl<'a, T: ?Sized + AsRef<U>, U: ?Sized + PartialEq> PartialEq<T> for MixedRefMut<'a, U> {
    fn eq(&self, other: &T) -> bool {
        (self as &U) == other.as_ref()
    }
}

impl<'a, T: Default> Default for MixedRef<'a, T> {
    fn default() -> Self {
        MixedRef::Owned(Default::default())
    }
}

impl<'a, T: Default> Default for MixedRefMut<'a, T> {
    fn default() -> Self {
        MixedRefMut::Owned(Default::default())
    }
}

impl<'a, T: ?Sized> From<&'a T> for MixedRef<'a, T> {
    fn from(r: &'a T) -> Self {
        MixedRef::Borrowed(r)
    }
}

impl<'a, T: ?Sized> From<&'a mut T> for MixedRefMut<'a, T> {
    fn from(r: &'a mut T) -> Self {
        MixedRefMut::Borrowed(r)
    }
}

impl<'a, T: ?Sized> From<Box<T>> for MixedRef<'a, T> {
    fn from(b: Box<T>) -> Self {
        MixedRef::Owned(b)
    }
}

impl<'a, T: ?Sized> From<Box<T>> for MixedRefMut<'a, T> {
    fn from(b: Box<T>) -> Self {
        MixedRefMut::Owned(b)
    }
}

impl<'a> From<String> for MixedRef<'a, str> {
    fn from(s: String) -> Self {
        Self::from(s.into_boxed_str())
    }
}

impl<'a> From<String> for MixedRefMut<'a, str> {
    fn from(s: String) -> Self {
        Self::from(s.into_boxed_str())
    }
}

impl<'a, T> From<Vec<T>> for MixedRef<'a, [T]> {
    fn from(v: Vec<T>) -> Self {
        Self::from(v.into_boxed_slice())
    }
}

impl<'a, T> From<Vec<T>> for MixedRefMut<'a, [T]> {
    fn from(v: Vec<T>) -> Self {
        Self::from(v.into_boxed_slice())
    }
}

impl<'a, T: ?Sized> From<MixedRefMut<'a, T>> for MixedRef<'a, T> {
    fn from(r: MixedRefMut<'a, T>) -> Self {
        match r {
            MixedRefMut::Owned(b) => MixedRef::Owned(b),
            MixedRefMut::Borrowed(r) => MixedRef::Borrowed(r),
        }
    }
}

impl<'a, T: ?Sized> Deref for MixedRef<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        match *self {
            MixedRef::Owned(ref b) => b,
            MixedRef::Borrowed(ref r) => r
        }
    }
}

impl<'a, T: ?Sized> Deref for MixedRefMut<'a, T> {
    type Target = T;

    fn deref(&self) -> &T {
        match *self {
            MixedRefMut::Owned(ref b) => b,
            MixedRefMut::Borrowed(ref r) => r
        }
    }
}

impl<'a, T: ?Sized> DerefMut for MixedRefMut<'a, T> {
    fn deref_mut (&mut self) -> &mut T {
        match *self {
            MixedRefMut::Owned(ref mut b) => b,
            MixedRefMut::Borrowed(ref mut r) => r
        }
    }
}

impl<'a, T: ?Sized> AsRef<T> for MixedRef<'a, T> {
    fn as_ref(&self) -> &T { self }
}

impl<'a, T: ?Sized> AsRef<T> for MixedRefMut<'a, T> {
    fn as_ref(&self) -> &T { self }
}

impl<'a, T: ?Sized> AsMut<T> for MixedRefMut<'a, T> {
    fn as_mut(&mut self) -> &mut T { self }
}

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

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

impl<'a, T: ?Sized> BorrowMut<T> for MixedRefMut<'a, T> {
    fn borrow_mut(&mut self) -> &mut T { self }
}

impl<'a, T: ?Sized> MixedRefMut<'a, T> {
    /// Downcasts `self` into a reference to immutable data.
    pub fn downcast(self) -> MixedRef<'a, T> {
        self.into()
    }
}