Uh oh!
There was an error while loading. Please reload this page.
Make ptr range iterable - #89900
Conversation
rust-highfive
commented
Oct 15, 2021
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
leonardo-m
commented
Oct 15, 2021
Isn't it better to convert the pair of pointers coming from C or C++ into something like a slice, for Rust usage? What do you need all those pointers for in Rust code? |
kennytm
commented
Oct 15, 2021
If let start = !1as*const[u8;2];let end = !0as*const[u8;2];(start..end).next(); |
dtolnay
commented
Oct 26, 2021
In FFI, I definitely don't think so. Materializing a reference (to a slice, or even to individual elements) is more prone to UB in Rust than directly working with the foreign pointers as pointers. One basically has to be an expert in Rust aliasing rules, which are not necessarily cogently written down at this point yet, in order to do it correctly. Whereas someone with basic C++ knowledge can write the pointer-based code and have it be correct. Compare: let start:*mutT = ...;let end:*mutT = ...;for ptr in start..end {// Only the preconditions of the C++ callee come into play, which a C++ dev// is already going to be comfortable reasoning about. Nothing about stacked// borrows, concurrency, initializedness, inhabitedness, alignedness, or other// Rust-isms is relevant.unsafe{cpp(ptr);}}letmut slice = std::slice::from_raw_parts_mut(...);// suddenly a pile of extremely subtle// invariants in order for this to be allowedfor elem in slice {unsafe{cpp(elem as*mutT);}// probably UB} |
dtolnay
commented
Oct 26, 2021
Good call. Hopefully this is fixable within the constraints of the |
This PR adds
std::iter::Stepimpls for*const Tand*mut T, enabling iteration over a range of pointers, such as produced by<[T]>::as_ptr_rangeand<[T]>::as_mut_ptr_range.It is common for systems interacting with C++ to need to use pointer ranges for iteration, as
begin/endpointer pairs are a design pattern in the C++ standard library.