Skip to main content

RCopy

Trait RCopy 

Source
pub trait RCopy {
    // Required methods
    fn copy(&self) -> Self;
    fn is_copy(&self) -> bool;
}
Expand description

Adapter trait for std::marker::Copy.

Indicates that a type can be cheaply copied (bitwise copy, no heap allocation). This is useful for R users to know that copying is O(1) and doesn’t involve deep cloning of heap data.

§Methods

  • copy() - Create a bitwise copy of this value
  • is_copy() - Returns true (useful for runtime type checking in R)

§Difference from RClone

Both RCopy and RClone create copies, but:

  • RCopy: Only for types where copying is cheap (stack-only, no heap)
  • RClone: For any clonable type (may involve heap allocation)

If a type implements both, prefer copy() when you know copies are frequent.

§Example

#[derive(Copy, Clone, ExternalPtr)]
struct Point { x: f64, y: f64 }

#[miniextendr]
impl RCopy for Point {}

In R:

p1 <- Point$new(1.0, 2.0)
p2 <- p1$copy()  # Cheap bitwise copy
p1$is_copy()       # TRUE

Required Methods§

Source

fn copy(&self) -> Self

Create a bitwise copy of this value.

For Copy types, this is always cheap (O(1), no heap allocation).

Source

fn is_copy(&self) -> bool

Check if this type implements Copy.

Always returns true for types implementing this trait. Useful for runtime type checking in R.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T: Copy> RCopy for T