Skip to content

Add uninit#61

Merged
ajz34 merged 8 commits intoRESTGroup:masterfrom
ajz34:uninit
Sep 19, 2025
Merged

Add uninit#61
ajz34 merged 8 commits intoRESTGroup:masterfrom
ajz34:uninit

Conversation

@ajz34
Copy link
Member

@ajz34 ajz34 commented Sep 18, 2025

This will be a major code refactor, probably an API breaking change.

This PR will focus on these topics:

  • Add uninit, which works like Rust's MaybeUninit.
  • Add assume_init, which works like assume_init but on the whole tensor instead of an element.
  • Change the internal functions that works with empty_impl to uninit_impl.

Fixes #7

@ajz34
Copy link
Member Author

ajz34 commented Sep 19, 2025

The correct way to use uninitialized vector, fill value of uninitialized tensor, then transform that to be a usual tensor, can be some code like:

#[test]
fn correct_code() {
    use rstsr_core::prelude::*;
    use rstsr_core::prelude_dev::*;

    // Generate a tensor of uninitialized vectors
    // Note that `MaybeUninit` will not call the destructor of `Vec<usize>`.
    let mut a: Tensor<MaybeUninit<Vec<usize>>> = rt::uninit([12]);
    // Some code to initialize the tensor elements
    a.iter_mut().enumerate().for_each(|(idx, x)| {
        *x = MaybeUninit::new(vec![idx]);
    });
    let a: Tensor<Vec<usize>> = unsafe { rt::assume_init(a) };
    println!("{:?}", a);
}

@ajz34 ajz34 merged commit 56c8bc9 into RESTGroup:master Sep 19, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Not proper usage of uninitialized vector causes UB and double free

1 participant