tinymist_std/concepts/
cow_mut.rs

1//! <https://github.com/rust-analyzer/rowan/blob/v0.16.1/src/cow_mut.rs>
2//!
3//! This module provides a `CowMut` type, which is a mutable version of `Cow`.
4//! Although it is strange that we can have a `CowMut`, because it should "copy
5//! on write", we also don't love the `Cow` API and use `Cow` without even
6//! touching its `DerefMut` feature.
7
8/// A mutable version of [Cow][`std::borrow::Cow`].
9#[derive(Debug)]
10pub enum CowMut<'a, T> {
11    /// An owned data.
12    Owned(T),
13    /// A borrowed mut data.
14    Borrowed(&'a mut T),
15}
16
17impl<T> std::ops::Deref for CowMut<'_, T> {
18    type Target = T;
19    fn deref(&self) -> &T {
20        match self {
21            CowMut::Owned(it) => it,
22            CowMut::Borrowed(it) => it,
23        }
24    }
25}
26
27impl<T> std::ops::DerefMut for CowMut<'_, T> {
28    fn deref_mut(&mut self) -> &mut T {
29        match self {
30            CowMut::Owned(it) => it,
31            CowMut::Borrowed(it) => it,
32        }
33    }
34}
35
36impl<T: Default> Default for CowMut<'_, T> {
37    fn default() -> Self {
38        CowMut::Owned(T::default())
39    }
40}