tinymist_std/concepts/
takable.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::sync::Arc;

/// Trait for values being taken.
pub trait TakeAs<T> {
    /// Takes the inner value if there is exactly one strong reference and
    /// clones it otherwise.
    fn take(self) -> T;
}

impl<T: Clone> TakeAs<T> for Arc<T> {
    fn take(self) -> T {
        match Arc::try_unwrap(self) {
            Ok(v) => v,
            Err(rc) => (*rc).clone(),
        }
    }
}