Skip to content

add wrapping_offset_from #244

Closed as not planned
Closed as not planned
@RalfJung

Description

@RalfJung

Proposal

Problem statement

Using wrapping_offset it is possible (even at const-time) to create pointers that are still associated with the original allocation they came from, but may point out-of-bounds of that allocation. One fairly common use of such an operation is for iterators over slices of ZST, where the begin and end pointers are re-interpreted as "the difference between them (in bytes) is the remaining number of elements" but where they actually point is irrelevant (since ZST values do not carry any data, only their count is relevant). This need an operation to compute the distance between two such pointers created by wrapping_offset. Currently, we do not offer such an operation: at runtime it is possible to cast the pointers to integers and do the subtraction there, but at compile time that will not work -- and also it seems preferable to avoid ptr-to-int casts when they are not actually needed.

Motivating examples or use cases

I mentioned the ZST iterator above. Also see rust-lang/rust#92512.

Solution sketch

Add a wrapping_offset_from that allows subtracting pointers even if they are out-of-bounds or have wrapped the address space, as long as they originate from the same allocation.

See rust-lang/rust#112837 for an implementation of this.

Alternatives

If we do nothing, implementing iterators in this way in const fn will remain impossible.

We could weaken the offset_from requirements to allow out-of-bounds and wrapping difference. However that would destroy the symmetry indicating by the naming of offset and offset_from.

Links and related work

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions