aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/util.rs
blob: 46f423c9155ddd5c7285ad6cd305e85239196c01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//! Internal utility functions.

use std::sync::Arc;

/// Helper for mutating `Arc<[T]>` (i.e. `Arc::make_mut` for Arc slices).
/// The underlying values are cloned if there are other strong references.
pub(crate) fn make_mut_arc_slice<T: Clone, R>(
    a: &mut Arc<[T]>,
    f: impl FnOnce(&mut [T]) -> R,
) -> R {
    if let Some(s) = Arc::get_mut(a) {
        f(s)
    } else {
        let mut v = a.to_vec();
        let r = f(&mut v);
        *a = Arc::from(v);
        r
    }
}