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
//! **Development in progress**
//!
//! This crate provides a trait [`AsIs`], an enum [`Is<'a, T>`], and variants of them.
//!
//! [`AsIs`]: trait.AsIs.html
//! [`Is<'a, T>`]: enum.Is.html

#![no_std]

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

#[cfg(feature = "alloc")]
use alloc::borrow::Cow;

mod to_owned;
pub use to_owned::ToOwned;

mod is;
mod is_cow;
mod is_mut;

pub mod ops;
pub mod ref_ops;

pub use is::Is;
pub use is_cow::IsCow;
pub use is_mut::IsMut;

mod foreign;
pub use foreign::{StringStub, VecStub};

#[cfg(test)]
mod tests;

use core::borrow::{Borrow, BorrowMut};

/// Used to do a cheap conversion into [`Is<'a, T>`] in a generic context.
///
/// [`Is<'a, T>`]: enum.Is.html
pub trait AsIs: Sized + Borrow<Self::Is> {
    /// The base type associated with both owned and borrowed data.
    type Is: ?Sized + ToOwned;

    /// Converts `self` into an [`Is<'a, Self::Is>`].
    ///
    /// [`Is<'a, Self::Is>`]: enum.Is.html
    #[allow(clippy::wrong_self_convention)]
    fn as_is<'a>(self) -> Is<'a, Self::Is>
    where
        Self: 'a;

    /// Immutably borrows from `self` by default, but can be overridden to clone `self` if
    /// appropriate.
    fn borrow_or_clone<B: ?Sized>(&self) -> IsCow<'_, B>
    where
        Self::Is: Borrow<B>,
        Owned<Self>: Borrow<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        IsCow::Borrowed(self.borrow().borrow())
    }

    /// Converts `self` into an [`Is<'a, B>`].
    ///
    /// [`Is<'a, B>`]: enum.Is.html
    fn into_is<'a, B: ?Sized>(self) -> Is<'a, B>
    where
        Self: 'a,
        Self::Is: BorrowMut<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        match self.as_is() {
            Is::Owned(x) => Is::Owned(x),
            Is::Borrowed(x) => Is::Borrowed(x.borrow()),
            Is::MutBorrowed(x) => Is::MutBorrowed(x.borrow_mut()),
        }
    }

    /// Converts `self` into an [`IsCow<'a, B>`].
    ///
    /// [`IsCow<'a, B>`]: enum.IsCow.html
    fn into_is_cow<'a, B: ?Sized>(self) -> IsCow<'a, B>
    where
        Self: 'a,
        Self::Is: Borrow<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        match self.as_is() {
            Is::Owned(x) => IsCow::Owned(x),
            Is::Borrowed(x) => IsCow::Borrowed(x.borrow()),
            Is::MutBorrowed(x) => IsCow::Borrowed((*x).borrow()),
        }
    }

    /// Converts `self` into an [`IsMut<'a, B>`] without cloning if possible.
    ///
    /// # Errors
    ///
    /// If `self` cannot be converted without cloning, returns `self` as is.
    ///
    /// [`IsMut<'a, B>`]: enum.IsMut.html
    fn try_into_is_mut<'a, B: ?Sized>(self) -> Result<IsMut<'a, B>, Is<'a, Self::Is>>
    where
        Self: 'a,
        Self::Is: BorrowMut<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        match self.as_is() {
            Is::Owned(x) => Ok(IsMut::Owned(x)),
            Is::Borrowed(x) => Err(Is::Borrowed(x)),
            Is::MutBorrowed(x) => Ok(IsMut::MutBorrowed(x.borrow_mut())),
        }
    }

    /// Converts `self` into an owned data without cloning if possible.
    ///
    /// # Errors
    ///
    /// If `self` cannot be converted without cloning, returns `self` as is.
    fn try_into_owned<'a>(self) -> Result<Owned<Self>, Is<'a, Self::Is>>
    where
        Self: 'a,
    {
        match self.as_is() {
            Is::Owned(x) => Ok(x),
            Is::Borrowed(x) => Err(Is::Borrowed(x)),
            Is::MutBorrowed(x) => Err(Is::MutBorrowed(x)),
        }
    }

    /// Converts `self` into a [`Cow<'a, B>`]. Only available if the `alloc` feature
    /// is enabled.
    ///
    /// [`Cow<'a, B>`]: https://doc.rust-lang.org/alloc/borrow/enum.Cow.html
    #[cfg(feature = "alloc")]
    fn into_cow<'a, B: ?Sized>(self) -> Cow<'a, B>
    where
        Self: 'a,
        Self::Is: Borrow<B>,
        B: ToOwned<Owned = Owned<Self>> + alloc::borrow::ToOwned<Owned = Owned<Self>>,
    {
        match self.as_is() {
            Is::Owned(x) => Cow::Owned(x),
            Is::Borrowed(x) => Cow::Borrowed(x.borrow()),
            Is::MutBorrowed(x) => Cow::Borrowed((*x).borrow()),
        }
    }
}

/// Used to do a cheap conversion into [`IsMut<'a, T>`] in a generic context.
///
/// [`IsMut<'a, T>`]: enum.IsMut.html
pub trait AsIsMut: AsIs + BorrowMut<Self::Is> {
    /// Converts `self` into an [`IsMut<'a, Self::Is>`].
    ///
    /// [`IsMut<'a, Self::Is>`]: enum.IsMut.html
    #[allow(clippy::wrong_self_convention)]
    fn as_is_mut<'a>(self) -> IsMut<'a, Self::Is>
    where
        Self: 'a;

    /// Mutably borrows from `self` by default, but clones `self` if [`self.borrow_or_clone()`] is overridden to clone `self`.
    ///
    /// [`self.borrow_or_clone()`]: trait.AsIs.html#method.borrow_or_clone
    fn borrow_mut_or_clone<B: ?Sized>(&mut self) -> IsMut<'_, B>
    where
        Self::Is: BorrowMut<B>,
        Owned<Self>: BorrowMut<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        match self.borrow_or_clone() {
            IsCow::Owned(x) => IsMut::Owned(x),
            IsCow::Borrowed(_) => IsMut::MutBorrowed(self.borrow_mut().borrow_mut()),
        }
    }

    /// Converts `self` into an [`IsMut<'a, B>`].
    ///
    /// [`IsMut<'a, B>`]: enum.IsMut.html
    fn into_is_mut<'a, B: ?Sized>(self) -> IsMut<'a, B>
    where
        Self: 'a,
        Self::Is: BorrowMut<B>,
        B: ToOwned<Owned = Owned<Self>>,
    {
        match self.as_is_mut() {
            IsMut::Owned(x) => IsMut::Owned(x),
            IsMut::MutBorrowed(x) => IsMut::MutBorrowed(x.borrow_mut()),
        }
    }
}

/// The owned type associated with a type.
pub type Owned<T> = <<T as AsIs>::Is as ToOwned>::Owned;

impl<T: ?Sized + ToOwned> AsIs for &T {
    type Is = T;

    fn as_is<'a>(self) -> Is<'a, Self::Is>
    where
        Self: 'a,
    {
        Is::Borrowed(self)
    }
}

impl<T: ?Sized + ToOwned> AsIs for &mut T {
    type Is = T;

    fn as_is<'a>(self) -> Is<'a, Self::Is>
    where
        Self: 'a,
    {
        Is::MutBorrowed(self)
    }
}

impl<T: ?Sized + ToOwned> AsIsMut for &mut T {
    fn as_is_mut<'a>(self) -> IsMut<'a, Self::Is>
    where
        Self: 'a,
    {
        IsMut::MutBorrowed(self)
    }
}