Trait Share
pub trait Share: Clone
A trait for types whose Clone operation creates another alias to the same
logical resource or shared state.
Share refines the meaning of Clone for types where cloning a value
creates another handle, reference, or alias to the same logical resource or
shared state, rather than an independent owned value. The distinction is
semantic, not operational: Share does not mean merely that cloning is
cheap, constant-time, allocation-free, or convenient.
Share is a third way to think about creating another usable value:
Copymay duplicate a value implicitly.Cloneexplicitly creates another value.Shareexplicitly creates another value that aliases the same underlying logical resource or shared state.
Share is not a replacement for either Copy or Clone, and neither
trait implies it. For example, integers are Copy but not Share, because
copying an integer creates an independent value. Likewise, not every cheap
Clone implementation is Share.
Shared references, Rc<T>, Arc<T>, Sender<T>, and SyncSender<T> are
examples of types that can be shared this way. Types such as Vec<T>,
String, Box<T>, owned collections, and similar owned values are not
Share, even though they implement Clone, because cloning them creates
independent owned storage or value ownership. Mutable references (&mut T)
are neither Clone nor Share, because you cannot have two active at once.
Calling share is equivalent to calling clone
for implementors, but communicates that the resulting value aliases the same
underlying resource. The share method is final, so implementors should
define the operation through Clone::clone and implement Share only when
those cloning semantics are clone-as-alias semantics.
Examples
use Cell;
use Share;
use Rc;
use ;
let value = 1;
let reference = &value;
assert!;
let rc = new;
let shared_rc = rc.share;
assert!;
shared_rc.set;
assert_eq!;
let arc = new;
let shared_arc = arc.share;
assert!;
shared_arc.store;
assert_eq!;
use Share;
use ;
let = channel;
let shared_sender = sender.share;
sender.send.unwrap;
shared_sender.send.unwrap;
let mut received = ;
received.sort;
assert_eq!;
let = sync_channel;
let shared_sync_sender = sync_sender.share;
sync_sender.send.unwrap;
shared_sync_sender.send.unwrap;
let mut received = ;
received.sort;
assert_eq!;
Provided Methods
Creates another alias to the same underlying resource or shared state.
This is equivalent to calling
Clone::clone. Useshareat call sites to make aliasing intent explicit; implementors define this operation throughClone::clone, not by overriding this method.
Implementors
impl<T: PointeeSized> Share for &T