pub trait CloneIn<'new_alloc>: Sized {
type Cloned;
// Required method
fn clone_in_impl(
&self,
with_semantic_ids: CloneInSemanticIds,
allocator: &'new_alloc Allocator,
) -> Self::Cloned;
// Provided methods
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned { ... }
fn clone_in_with_semantic_ids(
&self,
allocator: &'new_alloc Allocator,
) -> Self::Cloned { ... }
}Expand description
A trait to explicitly clone an object into an arena allocator.
As a convention Cloned associated type should always be the same as Self,
It’d only differ in the lifetime, Here’s an example:
impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for Struct<'old_alloc> {
type Cloned = Struct<'new_alloc>;
fn clone_in_impl(
&self,
with_semantic_ids: CloneInSemanticIds,
allocator: &'new_alloc Allocator,
) -> Self::Cloned {
Struct {
a: self.a.clone_in_impl(with_semantic_ids, allocator),
b: self.b.clone_in_impl(with_semantic_ids, allocator),
}
}
}Implementations of this trait on non-allocated items usually delegate to Clone::clone.
However, it isn’t guaranteed.
Required Associated Types§
Required Methods§
Sourcefn clone_in_impl(
&self,
with_semantic_ids: CloneInSemanticIds,
allocator: &'new_alloc Allocator,
) -> Self::Cloned
fn clone_in_impl( &self, with_semantic_ids: CloneInSemanticIds, allocator: &'new_alloc Allocator, ) -> Self::Cloned
Clone self into allocator, threading whether semantic ids should be preserved as a
runtime with_semantic_ids flag rather than as two separate methods.
This is the method that implementors provide.
clone_in and clone_in_with_semantic_ids are thin wrappers around it.
It exists so the CloneIn derive can emit a single recursive traversal that serves both:
clone_in:with_semantic_ids == CloneInSemanticIds::Withoutclone_in_with_semantic_ids:with_semantic_ids == CloneInSemanticIds::With
This is instead of monomorphizing two near-identical traversals over the whole AST.
ID fields copy existing values when with_semantic_ids == CloneInSemanticIds::With,
and reset to their default when CloneInSemanticIds::Without.
Prefer calling clone_in or clone_in_with_semantic_ids at call sites —
they name the intent and delegate here.
Provided Methods§
Sourcefn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned
Clone self into the given allocator. allocator may be the same one
that self is already in.
Sourcefn clone_in_with_semantic_ids(
&self,
allocator: &'new_alloc Allocator,
) -> Self::Cloned
fn clone_in_with_semantic_ids( &self, allocator: &'new_alloc Allocator, ) -> Self::Cloned
Almost identical as clone_in, but for some special type, it will also clone the semantic ids.
Please use this method only if you make sure semantic info is synced with the ast node.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".