Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

cuda-device: a safe `DisjointSlice::empty()`

Open Beginner friendly
#1,693 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
74/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
backend

Research direction

Start with the constructors in crates/cuda-device/src/disjoint.rs and check how DisjointSlice represents its pointer, length, and index-space witnesses. Add the safe empty value and the proposed Default implementation, keeping the pointer non-null and aligned; verify that witnesses yield None for an empty slice. Done means callers can use an empty DisjointSlice without an unsafe block.

Written by the indexing model from the issue text.

Description

What problem does this solve?
A device helper with an optional output takes a DisjointSlice<T> and skips writes when it's empty. Callers that don't want the output still have to build one, and every DisjointSlice constructor is unsafe (from_raw_parts_with_space, from_raw_parts, from_mut_slice in crates/cuda-device/src/disjoint.rs). So the call site carries an unsafe block with a SAFETY comment for a value that can't be misused:

// SAFETY: an empty slice covers no memory
let no_part = unsafe { DisjointSlice::from_mut_slice(&mut []) };
flash_attn::<...>(tiles, o, /* ... */, no_part);

A zero-length slice discharges every obligation in from_raw_parts's contract (valid for len elements, lifetime, exclusive access over ptr..ptr + 0), so the constructor can be safe.

Proposed API / behavior

impl<'a, T> DisjointSlice<'a, T> {
    /// An empty slice; every index-space witness on it yields `None`
    pub const fn empty() -> Self { /* NonNull::dangling(), len 0 */ }
}

impl<T> Default for DisjointSlice<'_, T> { fn default() -> Self { Self::empty() } }

let no_part = DisjointSlice::<f32>::empty();

A dangling, aligned pointer (rather than null) keeps as_mut_ptr() well-formed for zero-sized accesses. For the index-space-typed form, empty() would be defined for each space whose witnesses all check len.

Alternatives considered

  • unsafe { DisjointSlice::from_mut_slice(&mut []) }: works, but it adds an unsafe block per call site, and audits have to re-check each one.
  • Option<DisjointSlice<T>> parameters on the helper: also works, but it changes every helper signature and moves the test into the helper.

Additional context
Found porting sam3rs's flash-attention kernels to cuda-oxide (main 2d47e43, rechecked at 88e52d117). A shared flash_attn body has an optional partial-output buffer, and the kernels that don't use it pass an empty DisjointSlice.

Dominant language
Rust
Stars
3.9k
Forks
349
Avg merge
3d 20h
Merged PRs (30d)
81

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from NVIDIA/cuda-rust

All issues in NVIDIA/cuda-rust

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.