diondokter[m] has quit [Quit: Idle timeout reached: 172800s]
<JamesMunns[m]>
Instead of writing up all the ways you can accidentally do that, I'm working on a (currently pretty limited) crate to provide some alternatives: https://docs.rs/grounded
<JamesMunns[m]>
But if you have something you think you need `static mut` for, I'm definitely interested to figure out what building blocks would be useful to have available.
<JamesMunns[m]>
By the way, if anyone here is using `static mut` for anything, I'd be interested in hearing about it. IMO, it is *very* easy to accidentally do an undefined behavior with them.
temurumaru[m] has quit [Quit: Idle timeout reached: 172800s]
<Ecco>
New to rust: would those need to be wrapped in "unsafe" blocks? Or can there be UBs even in "safe" code?
<Ecco>
(those = uses of "static mut")
diondokter[m] has joined #rust-embedded
<diondokter[m]>
Ecco: You can only use them in unsafe blocks
<Ecco>
ok :)
<Ecco>
Good :)
<diondokter[m]>
<JamesMunns[m]> "By the way, if anyone here is..." <- > <@jamesmunns:beeper.com> By the way, if anyone here is using `static mut` for anything, I'd be interested in hearing about it. IMO, it is *very* easy to accidentally do an undefined behavior with them.... (full message at <https://catircservices.org/_matrix/media/v3/download/catircservices.org/NMCnyBixZulyuzkeebPSPnHw>)
<diondokter[m]>
* I've use them once in a while if I want to prove something to me. For example, I'm inspecting some generated assembly and I need a write to not be optimized away. A static mut is a quick way to get that in place of a let mut.
<diondokter[m]>
Another thing I can imagine, but haven't done myself so far, is having to have a global variable for compat with C. Say a C lib has a global extern variable, how would you create that un Rust without static mut?
<Ecco>
Very dumb question: what's the proper way to return an OutputPin from a function?
<Ecco>
I'd like to get some sample code where OutputPin are passed around
<diondokter[m]>
Oh I'll add it to the issue
<Ecco>
The sample code in embassy is great but super simple (that's a good thing), but then as a beginner I have no idea on how to pass OutputPin (or SpiDevice or whatever) around. I just get a shitton of unfathomable errors :-/
<JamesMunns[m]>
diondokter[m]: > <@diondokter:matrix.org> I've use them once in a while if I want to prove something to me. For example, I'm inspecting some generated assembly and I need a write to not be optimized away. A static mut is a quick way to get that in place of a let mut.... (full message at <https://catircservices.org/_matrix/media/v3/download/catircservices.org/pThXaVBHsIJIbeqZiwaTEzLn>)
<JamesMunns[m]>
however, the `UnsafeCell<MaybeUninit<T>>` or just `UnsafeCell<T>` can also be used as a non-mut static.
<diondokter[m]>
JamesMunns[m]: Ah right, you can use those because they are `#[repr(transparent)]`
<diondokter[m]>
Didn't think of that
<JamesMunns[m]>
IMO: basically anywhere you use `static mut` you should basically be using `UnsafeCell<T>` or `UnsafeCell<MaybeUninit<T>>`.
therealprof[m] has quit [Quit: Idle timeout reached: 172800s]
badrb[m] has joined #rust-embedded
<badrb[m]>
James Munns: just came across `postcard-rpc` (long-time `postcard` user!).Do you think MQTT would be a suitable option for transportation?
<badrb[m]>
s/transportation/transport/
<JamesMunns[m]>
Could be, it overlaps a bit, as postcard rpc provides topics/routing. You certainly could layer them, but it'd kind of be like udp over tcp :)
<badrb[m]>
* James Munns: just came across `postcard-rpc` (long-time `postcard` user!). Do you think MQTT would be a suitable option for transport?
<badrb[m]>
Yep, that's what I thought. I think I'll end up using postcard + MQTT.
<badrb[m]>
* Yep, that's what I thought. I think I'll end up using postcard + MQTT. I don't see any obvious benefit from postcard-rpc in this case.
<adamgreig[m]>
<JamesMunns[m]> "By the way, if anyone here is..." <- > <@jamesmunns:beeper.com> By the way, if anyone here is using `static mut` for anything, I'd be interested in hearing about it. IMO, it is *very* easy to accidentally do an undefined behavior with them.... (full message at <https://catircservices.org/_matrix/media/v3/download/catircservices.org/PnOfnAhQDPKvKrDwSVryjnav>)
<adamgreig[m]>
it sure is convenient to be able to return a `&'static str`
<GrantM11235[m]>
That's unsound, you will get UB if you call the function concurrently
<GrantM11235[m]>
wait, I didn't notice the critical section
<JamesMunns[m]>
It takes a critical section
<JamesMunns[m]>
Yeah, that actually looks pretty reasonable to me. Scoped access of the static mut makes it much harder to accidentally UB yourself
<adamgreig[m]>
yea, it rules out most shenanigans
<adamgreig[m]>
and it doesn't need maybeuninit because it is always initialised
<adamgreig[m]>
you could wrap it all in an unsafecell just to make it a non-mut static but i'm not sure it buys you anything? but i'm open to finding out there's some scenario where you end up violating aliasing or such
<adamgreig[m]>
once you've called it once you'll have given out the shared reference, but the shared reference doesn't exist until after you've finished writing it and you won't write it again
<JamesMunns[m]>
Yeah, I can't see anything wrong immediately. It's not that it's impossible to use static mut, it's just incredibly easy to misuse it
<GrantM11235[m]>
Could you use static critical_section::Mutexes instead?
<adamgreig[m]>
I can't do that and still return a &'static str
IlPalazzo-ojiisa has quit [Ping timeout: 240 seconds]
<GrantM11235[m]>
Maybe we should have a critical_section::OnceCell 🤔
<dirbaio[m]>
💯
<dirbaio[m]>
PRs welcome
starblue has quit [Ping timeout: 255 seconds]
<GrantM11235[m]>
Also, I notice that there are convenience methods for `Mutex<RefCell>`, but not for `Mutex<Cell>`. Is that just because no one has asked for it yet?
<dirbaio[m]>
yep
Darius has quit [Ping timeout: 256 seconds]
Darius has joined #rust-embedded
M9names[m] has quit [Quit: Idle timeout reached: 172800s]