laxcow 0.1.0

Clone-on-write smart pointer with relaxed trait constraints
Documentation
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Library provides [`LaxCow`] clone-on-write smart pointer with relaxed
//! trait constraints relative to [`Cow`]. The main difference being, [`LaxCow`]
//! is usable even if the owned type is not equal to the target type of
//! borrow type's implementation of [`ToOwned`] trait.
//!
//! Crate is totally `no_std`.

#![no_std]

extern crate alloc;

use alloc::borrow::{Cow, ToOwned};
use core::{
    borrow::Borrow,
    cmp::Ordering,
    fmt::{Debug, Display},
    hash::{Hash, Hasher},
    ops::Deref,
};

/// Clone-on-write smart pointer with relaxed trait constraints
/// relative to [`Cow`].
///
/// [`LaxCow`] does not constrain owned type in its type definition, but in
/// methods that specifically require this. Thus, Owned type is generic and
/// may need to be explicitly defined when instantiating. Also, [`LaxCow`]
/// isn't strictly clone-on-write as not all instances of it support
/// writing i.e. mutable access.
///
/// # Examples
///
/// ## Simple usage
///
/// ```
/// use laxcow::LaxCow;
///
/// let lc = LaxCow::Borrowed("foobar");
///
/// let lc2 = lc.clone();
/// assert_eq!(lc2, LaxCow::<_, String>::Borrowed("foobar"));
///
/// let owned = lc.into_owned();
/// assert_eq!(owned, "foobar".to_owned());
/// ```
///
/// ## Usage not possible with [`Cow`]
///
/// Storing a borrowed struct which doesn't implement `Clone`.
/// This is possible because [`LaxCow::Owned`] variant is not restricted
/// by the [`LaxCow::Borrowed`] variant via [`ToOwned`] trait.
///
/// ```
/// use laxcow::LaxCow;
///
/// struct Foo;
///
/// let laxcow = LaxCow::<_, ()>::Borrowed(&Foo);
/// ```
///
/// ## [`Cow`] definition by wrapping [`LaxCow`]
///
/// ```
/// use laxcow::LaxCow;
///
/// struct Cow<'a, T: ?Sized + ToOwned>(LaxCow::<'a, T, T::Owned>);
/// ```
pub enum LaxCow<'a, B: ?Sized, O = B> {
    Borrowed(&'a B),
    Owned(O),
}

impl<B: ?Sized, O> LaxCow<'_, B, O> {
    /// Returns `true` if [`LaxCow`] contains borrowed item, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// // We don't care what owned type is as it is not used.
    /// let borrowed = LaxCow::<i32, ()>::Borrowed(&42);
    ///
    /// assert!(borrowed.is_borrowed());
    /// ```
    pub const fn is_borrowed(&self) -> bool {
        match self {
            Self::Borrowed(_) => true,
            Self::Owned(_) => false,
        }
    }

    /// Returns `true` if [`LaxCow`] contains owned item, `false` otherwise.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// // We don't care what borrowed type is as it is not used.
    /// let owned = LaxCow::<(), i32>::Owned(42);
    ///
    /// assert!(owned.is_owned());
    /// ```
    pub const fn is_owned(&self) -> bool {
        !self.is_borrowed()
    }

    /// Returns mutable reference to the owned item.
    ///
    /// If the item is currently borrowed, it will converted
    /// into owned via [`ToOwned`] trait before returning mutable
    /// reference to it.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// let mut borrowed = LaxCow::Borrowed(&42);
    /// let mut owned = LaxCow::<i32, _>::Owned(42);
    ///
    /// *borrowed.to_mut() += 10;
    /// *owned.to_mut() += 10;
    ///
    /// assert_eq!(borrowed, LaxCow::Owned(52));
    /// assert_eq!(owned, LaxCow::Owned(52));
    /// ```
    pub fn to_mut(&mut self) -> &mut O
    where
        B: ToOwned<Owned = O>,
    {
        match self {
            Self::Borrowed(borrowed) => {
                *self = Self::Owned(borrowed.to_owned());
                match self {
                    Self::Owned(owned) => owned,
                    Self::Borrowed(_) => unreachable!(),
                }
            }
            Self::Owned(owned) => owned,
        }
    }

    /// Returns mutable reference to the owned item wrapped in [`Some`] if the item is owned.
    /// Otherwise, if the item is borrowed, [`None`] will be returned.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// let mut borrowed = LaxCow::<_, String>::Borrowed("foobar");
    /// let mut owned = LaxCow::<str, _>::Owned("foobar".to_owned());
    ///
    /// assert_eq!(borrowed.as_owned_mut(), None);
    /// assert_eq!(owned.as_owned_mut(), Some(&mut "foobar".to_owned()));
    /// ```
    pub fn as_owned_mut(&mut self) -> Option<&mut O> {
        match self {
            Self::Borrowed(_) => None,
            Self::Owned(owned) => Some(owned),
        }
    }

    /// Consumes [`LaxCow`] and returns owned item as is, or borrowed
    /// item via [`ToOwned`] trait conversion.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// let borrowed: LaxCow<_, String> = LaxCow::Borrowed("foobar");
    /// let owned: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());
    ///
    /// assert_eq!(borrowed.into_owned(), "foobar".to_owned());
    /// assert_eq!(owned.into_owned(), "foobar".to_owned());
    /// ```
    pub fn into_owned(self) -> O
    where
        B: ToOwned<Owned = O>,
    {
        match self {
            Self::Borrowed(borrowed) => borrowed.to_owned(),
            Self::Owned(owned) => owned,
        }
    }

    /// Returns owned item wrapped in [`Some`], or [`None`] if the item is borrowed.
    ///
    /// # Examples
    /// ```
    /// use laxcow::LaxCow;
    ///
    /// let mut borrowed = LaxCow::<_, String>::Borrowed("foobar");
    /// let mut owned = LaxCow::<str, _>::Owned("foobar".to_owned());
    ///
    /// assert_eq!(borrowed.try_into_owned(), None);
    /// assert_eq!(owned.try_into_owned(), Some("foobar".to_owned()));
    /// ```
    pub fn try_into_owned(self) -> Option<O> {
        match self {
            Self::Borrowed(_) => None,
            Self::Owned(owned) => Some(owned),
        }
    }
}

impl<B: ?Sized, O> AsRef<B> for LaxCow<'_, B, O>
where
    O: AsRef<B>,
{
    fn as_ref(&self) -> &B {
        match self {
            Self::Borrowed(borrowed) => borrowed,
            Self::Owned(owned) => owned.as_ref(),
        }
    }
}

impl<B: ?Sized, O> Clone for LaxCow<'_, B, O>
where
    O: Clone,
{
    fn clone(&self) -> Self {
        match self {
            Self::Borrowed(borrowed) => Self::Borrowed(borrowed),
            Self::Owned(owned) => Self::Owned(owned.clone()),
        }
    }
}

impl<B: ?Sized, O> Debug for LaxCow<'_, B, O>
where
    B: Debug,
    O: Debug,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Borrowed(borrowed) => Debug::fmt(borrowed, f),
            Self::Owned(owned) => Debug::fmt(owned, f),
        }
    }
}

impl<B: ?Sized, O> Default for LaxCow<'_, B, O>
where
    O: Default,
{
    fn default() -> Self {
        Self::Owned(Default::default())
    }
}

impl<B: ?Sized, O> Deref for LaxCow<'_, B, O>
where
    O: Borrow<B>,
{
    type Target = B;

    fn deref(&self) -> &B {
        match self {
            Self::Borrowed(borrowed) => borrowed,
            Self::Owned(owned) => owned.borrow(),
        }
    }
}

impl<B: ?Sized, O> Display for LaxCow<'_, B, O>
where
    B: Display,
    O: Display,
{
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Borrowed(borrowed) => Display::fmt(borrowed, f),
            Self::Owned(owned) => Display::fmt(owned, f),
        }
    }
}

impl<'a, B: ?Sized, O> From<Cow<'a, B>> for LaxCow<'a, B, O>
where
    B: ToOwned<Owned = O>,
{
    fn from(cow: Cow<'a, B>) -> Self {
        match cow {
            Cow::Borrowed(borrowed) => LaxCow::Borrowed(borrowed),
            Cow::Owned(owned) => LaxCow::Owned(owned),
        }
    }
}

impl<'a, B: ?Sized, O> From<LaxCow<'a, B, O>> for Cow<'a, B>
where
    B: ToOwned<Owned = O>,
{
    fn from(laxcow: LaxCow<'a, B, O>) -> Self {
        match laxcow {
            LaxCow::Borrowed(borrowed) => Cow::Borrowed(borrowed),
            LaxCow::Owned(owned) => Cow::Owned(owned),
        }
    }
}

impl<B: ?Sized, O> Hash for LaxCow<'_, B, O>
where
    O: Borrow<B>,
    B: Hash,
{
    #[inline]
    fn hash<H: Hasher>(&self, state: &mut H) {
        Hash::hash(&**self, state)
    }
}

impl<B: ?Sized, O> Eq for LaxCow<'_, B, O>
where
    B: Eq,
    O: Borrow<B>,
{
}

impl<B: ?Sized, O> Ord for LaxCow<'_, B, O>
where
    O: Borrow<B>,
    B: Ord,
{
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        Ord::cmp(&**self, &**other)
    }
}

impl<'a, 'b, B: ?Sized, O, B2: ?Sized, O2> PartialEq<LaxCow<'a, B2, O2>> for LaxCow<'b, B, O>
where
    B: PartialEq<B2>,
    O: Borrow<B>,
    O2: Borrow<B2>,
{
    #[inline]
    fn eq(&self, other: &LaxCow<'a, B2, O2>) -> bool {
        PartialEq::eq(&**self, &**other)
    }
}

impl<B: ?Sized, O> PartialOrd for LaxCow<'_, B, O>
where
    B: PartialOrd,
    O: Borrow<B>,
{
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        PartialOrd::partial_cmp(&**self, &**other)
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    use alloc::{format, string::String};
    use std::collections::hash_map::DefaultHasher;

    use super::*;

    #[test]
    fn send_sync() {
        fn foobar(_: impl Send + Sync) {}

        let laxcow = LaxCow::<i32>::Borrowed(&42);
        foobar(laxcow);
    }

    #[test]
    fn debug() {
        let laxcow: LaxCow<str, ()> = LaxCow::Borrowed("foobar");
        assert_eq!(format!("{laxcow:?}"), "\"foobar\"");

        let laxcow: LaxCow<(), String> = LaxCow::Owned("foobar".to_owned());
        assert_eq!(format!("{laxcow:?}"), "\"foobar\"");
    }

    #[test]
    fn display() {
        let laxcow: LaxCow<str, String> = LaxCow::Borrowed("foobar");
        assert_eq!(format!("{laxcow:?}"), "\"foobar\"");

        let laxcow: LaxCow<str, String> = LaxCow::Owned("foobar".to_owned());
        assert_eq!(format!("{laxcow:?}"), "\"foobar\"");
    }

    #[test]
    fn clone() {
        let cow: LaxCow<_, String> = LaxCow::Borrowed("foobar");
        assert_eq!(cow.clone(), LaxCow::<_, String>::Borrowed("foobar"));

        let cow: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());
        assert_eq!(cow.clone(), LaxCow::<str, _>::Owned("foobar".to_owned()));
    }

    #[test]
    fn as_ref() {
        let cow: LaxCow<_, String> = LaxCow::Borrowed("foobar");
        assert_eq!(cow.as_ref(), "foobar");

        let cow: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());
        assert_eq!(cow.as_ref(), "foobar");
    }

    #[test]
    fn default() {
        let cow = LaxCow::<str, String>::default();
        assert_eq!(cow, LaxCow::<str, String>::Owned(Default::default()));
    }

    #[test]
    fn deref() {
        let cow = LaxCow::<_, String>::Borrowed("foobar");
        assert_eq!(cow.deref(), "foobar");

        let cow = LaxCow::<str, _>::Owned("foobar".to_owned());
        assert_eq!(cow.deref(), "foobar");
    }

    #[test]
    fn from_cow_into_laxcow() {
        let cow = Cow::Borrowed("foobar");
        let laxcow = LaxCow::from(cow);
        assert_eq!(laxcow, LaxCow::<_, String>::Borrowed("foobar"));

        let cow: Cow<str> = Cow::Owned("foobar".to_owned());
        let laxcow = LaxCow::from(cow);
        assert_eq!(laxcow, LaxCow::<str, _>::Owned("foobar".to_owned()));
    }

    #[test]
    fn into_cow_from_laxcow() {
        let laxcow = LaxCow::Borrowed("foobar");
        let cow = Cow::from(laxcow);
        assert_eq!(cow, Cow::Borrowed("foobar"));

        let laxcow = LaxCow::Owned("foobar".to_owned());
        let cow: Cow<str> = Cow::from(laxcow);
        assert_eq!(cow, Cow::<str>::Owned("foobar".to_owned()));
    }

    #[test]
    fn eq() {
        let borrowed: LaxCow<_, String> = LaxCow::Borrowed("foobar");
        let owned: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());

        assert_eq!(borrowed, owned);

        let borrowed: LaxCow<i32> = LaxCow::Borrowed(&42);
        let owned = LaxCow::Owned(42);

        assert_eq!(borrowed, owned);
    }

    #[test]
    fn ord() {
        let borrowed: LaxCow<i32> = LaxCow::Borrowed(&42);
        let owned = LaxCow::Owned(100);
        assert_eq!(borrowed.cmp(&owned), Ordering::Less);

        let borrowed: LaxCow<i32> = LaxCow::Borrowed(&42);
        let owned = LaxCow::Owned(42);
        assert_eq!(borrowed.cmp(&owned), Ordering::Equal);

        let borrowed: LaxCow<i32> = LaxCow::Borrowed(&42);
        let owned = LaxCow::Owned(1);
        assert_eq!(borrowed.cmp(&owned), Ordering::Greater);
    }

    #[test]
    fn hash() {
        let borrowed: LaxCow<_, String> = LaxCow::Borrowed("foobar");
        let owned: LaxCow<str, _> = LaxCow::Owned("foobar".to_owned());

        fn hash_helper(h: impl Hash) -> u64 {
            let mut hasher = DefaultHasher::new();
            h.hash(&mut hasher);
            hasher.finish()
        }

        assert_eq!(borrowed, owned);
        assert_eq!(hash_helper(borrowed), hash_helper(owned));
    }
}