sp_im/
shared.rs

1// adapted from https://github.com/bodil/im-rs/blob/10.2.0/src/shared.rs
2
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7//! Automatic `Arc` wrapping.
8
9use alloc::sync::Arc;
10
11/// # Automatic `Arc` wrapping
12///
13/// The `Shared` trait provides automatic wrapping for things which
14/// take [`Arc`][alloc::sync::Arc]s, meaning that anything which takes
15/// an argument of type `Shared<A>` will accept either an `A` or an
16/// `Arc<A>`.
17///
18/// Because everything stored in `im`'s persistent data structures is
19/// wrapped in [`Arc`][alloc::sync::Arc]s, `Shared` makes you have to
20/// worry less about whether what you've got is an `A` or an `Arc<A>`
21/// or a reference to such - the compiler will just figure it out for
22/// you, which is as it should be.
23///
24/// [alloc::sync::Arc]: https://doc.rust-lang.org/alloc/sync/struct.Arc.html
25pub trait Shared<A> {
26  /// Get a new Arc pointer for this value
27  fn shared(self) -> Arc<A>;
28}
29
30impl<A> Shared<A> for A {
31  fn shared(self) -> Arc<A> { Arc::new(self) }
32}
33
34impl<'a, A> Shared<A> for &'a A
35where A: Clone
36{
37  fn shared(self) -> Arc<A> { Arc::new(self.clone()) }
38}
39
40impl<A> Shared<A> for Arc<A> {
41  fn shared(self) -> Arc<A> { self }
42}
43
44impl<'a, A> Shared<A> for &'a Arc<A> {
45  fn shared(self) -> Arc<A> { self.clone() }
46}