Questions tagged [maybeuninit]

In Rust, MaybeUninit is a wrapper type that serves to enable unsafe code to deal with uninitialized data. It is a signal to the compiler indicating that the data here might not be initialized.

Rust documentation: https://doc.rust-lang.org/stable/std/mem/union.MaybeUninit.html

9 questions
3
votes
2 answers

What does "uninitialized" mean in the context of FFI?

I'm writing some GPU code for macOS using the metal crate. In doing so, I allocate a Buffer object by calling: let buffer = device.new_buffer(num_bytes, MTLResourceOptions::StorageModeShared) This FFIs to Apple's Metal API, which allocates a region…
Rick Weber
  • 53
  • 6
2
votes
1 answer

Is it safe to clone an `Rc>` as `Rc` with uninitilized data?

Currently in my project I have a struct that looks something like this: pub struct Ptr(Rc>>); pub(crate) type EntList = Vec>; struct Entities { pub entity_a: EntList, pub entity_b: EntList, …
pigeonhands
  • 3,066
  • 15
  • 26
1
vote
1 answer

MaybeUninit versus Union

Does the compiler behave differently with MaybeUninit or with a union with the same structure? If yes, what additional things does it do with MaybeUninit? Specifically, is it the same (except for the different methods) to use original…
FreD
  • 393
  • 2
  • 12
1
vote
1 answer

Creating a `Pin>` in Rust when `[T; N]` is too large to be created on the stack

Generalized Question How can I implement a general function pinned_array_of_default in stable Rust where [T; N] is too large to fit on the stack? fn pinned_array_of_default() -> Pin> { …
Locke
  • 7,626
  • 2
  • 21
  • 41
1
vote
1 answer

Replace mem::uninitialized with MaybeUninit

I'm working on a project around embedded systems that needs to use filesystem. I want to use liitlefs crate in rust but it uses mem::uninitialized that deprecated since 1.39.0 . https://github.com/brandonedens/rust-littlefs pub struct LittleFs
p3zhy
  • 81
  • 8
0
votes
1 answer

cannot move out of type `[T; D]`, a non-copy array

I'm initializing an array using code called through FFI and thus I'm using an array if MaybeUninit to represent that data. It looks something like this: use std::mem::MaybeUninit; #[allow(unused)] fn init_with_ffi(data: &mut [T]) { …
Emil Eriksson
  • 2,110
  • 1
  • 21
  • 31
0
votes
1 answer

What is the difference between `MaybeUninit::<[MaybeUninit; N]>::uninit().assume_init()` and `[MaybeUninit::uninit(); N]`?

The Rust documentation for MaybeUninit explain that an array should be initialized with... let mut array: [MaybeUninit; N] = unsafe { MaybeUninit::<[MaybeUninit; N]>::uninit().assume_init() }; However, could I not just initialize the array…
scottwillmoore
  • 450
  • 2
  • 9
0
votes
1 answer

How to drop a MaybeUninit of vector or array which is partially initialized?

I'm looking for information and good practices for using MaybeUninit to directly initialize collections (typically arrays or vectors) and drop them properly if initialization failed. Thanks to the API examples, I was able to get by fairly quickly…
FreD
  • 393
  • 2
  • 12
-1
votes
1 answer

Rust fast stack implementation without unnecessary memset?

I need a fast stack in Rust. Millions of these need to be created/destroyed per second and each of them need only a fixed depth. I'm trying to squeeze as much speed as I can. I came up with the following (basically textbook stack…
user1002430