-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make pointer::byte_offset_from
more generic
#103489
Conversation
Hey! It looks like you've submitted a new PR for the library teams! If this PR contains changes to any Examples of
|
(rust-highfive has picked a reviewer for you, use r? to override) |
I'm concerned that this makes the API a little easier to misuse. Both pointers need to be in the same allocation with the same provenance, which seems "less likely" if the types are different. I could imagine someone accidentally passing in the wrong pointer, and having their mistake caught due to a type error. If it's rare in practice to call this with two differently-typed pointers, than I think it would be better tor require an explicit cast on the caller side. |
The problem I have here is that if the types are the same, why wouldn't the caller be using But I guess it's hard to make a good case either way since there appear to be literally zero uses of So maybe this really needs a code audit to find places that could use this instead of whatever it's currently doing. That accidentally ended up really helpful in #95837, for example, showing that actually nothing wanted |
@rustbot label +I-libs-api-nominated +S-waiting-on-team Hi libs-api! I brought this up, so obviously I think it's a good idea, but given objections raised above I'd like to get extra thoughts on it. Would you rather this stay homogeneous, or should it accept mixed types of pointers? |
We discussed this very briefly in the libs-api meeting. This change looks fine to us. :) |
With the ok for nightly from libs-api, |
… r=scottmcm Make `pointer::byte_offset_from` more generic As suggested by rust-lang#96283 (comment) (cc `@scottmcm),` make `pointer::byte_offset_from` work on pointers of different types. `byte_offset_from` really doesn't care about pointer types, so this is totally fine and, for example, allows patterns like this: ```rust ptr::addr_of!(x.b).byte_offset_from(ptr::addr_of!(x)) ``` The only possible downside is that this removes the `T` == `U` hint to inference, but I don't think this matter much. I don't think there are a lot of cases where you'd want to use `byte_offset_from` with a pointer of unbounded type (and in such cases you can just specify the type). `@rustbot` label +T-libs-api
… r=scottmcm Make `pointer::byte_offset_from` more generic As suggested by rust-lang#96283 (comment) (cc ``@scottmcm),`` make `pointer::byte_offset_from` work on pointers of different types. `byte_offset_from` really doesn't care about pointer types, so this is totally fine and, for example, allows patterns like this: ```rust ptr::addr_of!(x.b).byte_offset_from(ptr::addr_of!(x)) ``` The only possible downside is that this removes the `T` == `U` hint to inference, but I don't think this matter much. I don't think there are a lot of cases where you'd want to use `byte_offset_from` with a pointer of unbounded type (and in such cases you can just specify the type). ``@rustbot`` label +T-libs-api
… r=scottmcm Make `pointer::byte_offset_from` more generic As suggested by rust-lang#96283 (comment) (cc ```@scottmcm),``` make `pointer::byte_offset_from` work on pointers of different types. `byte_offset_from` really doesn't care about pointer types, so this is totally fine and, for example, allows patterns like this: ```rust ptr::addr_of!(x.b).byte_offset_from(ptr::addr_of!(x)) ``` The only possible downside is that this removes the `T` == `U` hint to inference, but I don't think this matter much. I don't think there are a lot of cases where you'd want to use `byte_offset_from` with a pointer of unbounded type (and in such cases you can just specify the type). ```@rustbot``` label +T-libs-api
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#103484 (Add `rust` to `let_underscore_lock` example) - rust-lang#103489 (Make `pointer::byte_offset_from` more generic) - rust-lang#104193 (Shift no characters when using raw string literals) - rust-lang#104348 (Respect visibility & stability of inherent associated types) - rust-lang#104401 (avoid memory leak in mpsc test) - rust-lang#104419 (Fix test/ui/issues/issue-30490.rs) - rust-lang#104424 (rustdoc: remove no-op CSS `.popover { font-size: 1rem }`) - rust-lang#104425 (rustdoc: remove no-op CSS `.main-header { justify-content }`) - rust-lang#104450 (Fuchsia test suite script fix) - rust-lang#104471 (Update PROBLEMATIC_CONSTS in style.rs) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
As suggested by #96283 (comment) (cc @scottmcm), make
pointer::byte_offset_from
work on pointers of different types.byte_offset_from
really doesn't care about pointer types, so this is totally fine and, for example, allows patterns like this:The only possible downside is that this removes the
T
==U
hint to inference, but I don't think this matter much. I don't think there are a lot of cases where you'd want to usebyte_offset_from
with a pointer of unbounded type (and in such cases you can just specify the type).@rustbot label +T-libs-api