How to instantiate a struct with private fields?

In libc for macos we have a struct:

    pub struct sched_param {
        pub sched_priority: ::c_int,
        __opaque: [::c_char; 4],

How do I instantiate this struct? I only need to set and use the sched_priority parameter. In C I'd just allocate enough memory for it and set the first sizeof(int) bytes to the desired value. How to do the same in rust?

Try MaybeUninit::<sched_param>::zeroed().assume_init(). If you'd really like to avoid the extra 4 byte write, you could use MaybeUninit::uninit(), but don't call assume_init() on it until you use raw pointers to write that first int.

Thanks for the reply. I have also just noticed it is not marked as repr(C), does that actually mean that sched_priority will start at the zero offset of the object's address? Oh wait, I will already have the object to work with, so there is nothing to worry about.

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.