Skip to content

Add #[derive(DeviceCopy)] for CUDA transfer-safe data types #192

Closed

Description

@niklebedenko

Problem

cuda_core::DeviceCopy is the safety marker used for values that may be copied to and from device memory. Today users must write unsafe impl DeviceCopy by hand for their own plain-data structs, even when every field already implements DeviceCopy.

That is both noisy and easy to get wrong: a manual impl can accidentally skip the field-by-field safety check that the marker trait relies on.

Why this matters

DeviceCopy is an unsafe marker trait, so the ergonomic path should make the safe case easy while still enforcing the same rule: a composite type can be DeviceCopy only if every stored field is also DeviceCopy. A derive macro can encode that rule mechanically and reduce hand-written unsafe impls.

For a comparable example, see Rust CUDA's #[derive(DeviceCopy)] implementation.

Expected behavior

Users should be able to write #[derive(DeviceCopy)] for transfer-safe structs, enums, and unions. The derive should be available through use cuda_core::DeviceCopy;, matching the usual trait-plus-derive ergonomics pattern, and invalid field types should fail at compile time.

Acceptance criteria

  • cuda-macros provides a DeviceCopy derive macro.
  • cuda-core re-exports the derive next to the DeviceCopy trait.
  • The generated impl requires all field types to implement DeviceCopy.
  • Generic type parameters receive the required DeviceCopy bounds.
  • #[derive(DeviceCopy)] works for named structs, tuple structs, enums, unions, and generic structs whose fields are DeviceCopy.
  • The derive is available through use cuda_core::DeviceCopy;, matching the common trait-plus-derive ergonomics pattern.
  • Compile-pass coverage exercises representative struct, enum, union, and generic forms.
  • Compile-fail coverage proves that a host reference field is rejected because it is not DeviceCopy.

Activity

  1. added
    enhancement
    host-apis
    safety
    on Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementhost-apissafety

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions