Assume_init on MaybeUninit without address change

Background: I have a FFI c-function that takes an uninitialized pointer to a struct ( I realized it with a variable MaybeUninit) and initializes it. In this function call the address is stored for an interrupt . But when I apply the assert_init to the variable afterwards, the address of this changes. Is there a way to keep the address because I need this "original" struct to interact with the interrupt.

Words are often imprecise. It might make your question more easy to understand if there was some code. E. g. the concrete types involved at both sides, how you get the address, possibly even a full reproducing example. Feel free to a look here to learn how to post code blocks in markdown.

Regarding imprecise wording: In particular, “uninitialized pointer to a struct” sounds like MaybeUninit<*mut Struct> to me, but that would most likely be a bad idea to use, and as soon as I don't take your wording literal anymore, there's a huge number of different things yout could actually be referring to. Also, I believe you may be referring to the “assume_init” method instead of something called “assert_init”.

The heavy approach is encapsulated by the moveit crate, via trait New.

See the docs, or the talk, or the two blog posts:

https://mcyoung.xyz/2021/04/26/move-ctors/

https://mcyoung.xyz/2021/12/19/move-ctors-2/

In short, Pin<&mut MaybeUninit<T>> and a lot of care.

1 Like
let mut spi: MaybeUninit<hal::cyhal_spi_t> = core::mem::MaybeUninit::uninit();
 // address of spi something like 0x800fc84
let mut result = hal::cyhal_spi_init( // function fills spi with values
  spi.as_mut_ptr(), 
  );
//  
let mut spi: hal::cyhal_spi_t =  spi.assume_init();
// address of spi something like 0x800fe3c but not the same which causes problems

Is using assume_init_mut feasible?

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.