How to convert this C code?

Sure, but the OP reported that they saw crashes when the data was put into writable memory instead of read-only flash.

Indeed, I'm working in an embedded env and if the memory is mutable, it will end in the RAM whereas the closed source C function I'm using expect a structure sitting in the Flash.

You can try some #[link_section = "..."] on the static declarations to get them located in the section of your choice, maybe with extra linker arguments.

That being said, that's an issue of linkage, so whether C or Rust is used to compile should not affect that.

Since I haven't yet worked on embedded devices, I don't know much about link scripts, but let's hope somebody else can help you with that.

Why do you need (interior) mutability in the first place?

I don't, it was introduced because of the conversion issue. The C struct is const. I didn't have the chance to test your solution yet :slightly_smiling_face: (Fighting with the hardware and a proprietary lib to make the radio work with Rust)

The thing is I don't understand how interior mutability helps with anything here.^^

You lost me ^^'

Why is there any static mut or UnsafeCell in the first place? What is being mutated here? I thought this just constructs some static data?

You said "because of the conversion issue", please elaborate as I have no idea what you mean by that.

I'll ask in another way: if you simply make it static, without mut or UnsafeCell, will it work, or something will break/not compile?

As I was mimicing the C construct, I tried to cast a pointer into a u32 which is not allowed in an const struct in Rust. So I tried to init the needed fields in a function, hence the mut. But your approach should avoid this. For the moment, I kept the C code and use the global var in Rust. It's easier as this piece of C code is generated by Silabs' tooling. Once stabilized, I'll have a look at converting it in Rust using your suggestion :slight_smile:

When I ported the code from the .h in your OP, some global structs were not marked const, meaning that their being mutable is part of their API. Hence the Mut wrapper.

If your global structs do not need to be mutable, then the .h file should be adjusted to mark them const, and the corresponding Rust code no longer needs the Mut wrapper.

uh, that's true, I missed that one of them is not const. I need to verify that Monday

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.