All the answers to this question about passing an array from C to Rust use std::slice::from_raw_parts to convert the raw C pointer and some length information into a Rust. In an embedded context (MOS 6502 in my case), there might not be a std library available. So in a #![no_std] context, what is the nicest way of passing an array from C to Rust for (potentially mutable) processing?
-
1I added the [rust-no-std] tag to your question and needed to remove the [pointers] tag as the question would have exceeded the five tags limit after my edit. If you feel that the [pointers] tag is more relevant to the question than [rust-no-std], feel free to revert (but note that I also fixed a typo – you probably don't want to revert that part of the edit either way).Elias Holzmann– Elias Holzmann2021-09-02 13:05:09 +00:00Commented Sep 2, 2021 at 13:05
-
@EliasHolzmann thanks, the [rust-no-std] tag is exactly what I should have used, I just didn't know about it.Cactus– Cactus2021-09-02 13:06:58 +00:00Commented Sep 2, 2021 at 13:06
Add a comment
|
1 Answer
While std::slice::from_raw_parts is not available in a no_std environment, core::slice::from_raw_parts is available, so you can simply use that.
In general, anything that is in std and not somehow platform dependent (so, not I/O, heap allocation and the like) should also be available in no_std environments through the core crate.
1 Comment
Tim Diekmann
Additionally, since Rust 2018 you can also use
core in non-no_std crates. std::slice::from_raw_parts is just an alias to core::slice::from_raw_parts, which can be used directly all the time.