Skip to content

Generalize SizeEq to SizeFrom, support shrinking #2580

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Jun 9, 2025

Generalize SizeEq, renaming it to SizeFrom, and supporting casts
which shrink rather than preserve referent size.

This requires fairly deep surgery in our transmute taxonomy. We can no
longer rely on SizeFrom::cast_from_raw preserving referent size, which
means that all pointer transmutes must now reason about two types of
transmutes:

  • Size-shrinking transmutes (in which the source value is larger than
    the destination value)
  • Overwriting transmutes (in which the source value is larger than the
    destination value and the destination permits mutation). In an
    overwriting transmute, the resulting value is a concatenation of a
    valid T and the suffix of a valid U.

(Note that size-preserving transmutes can be viewed as a special case of
size-shrinking transmutes.)

In order to support these semantics, we split TransmuteFrom into two
traits: TransmuteFrom (which supports shriking transmutes) and
TransmuteOverwrite (which supports overwriting transmutes).

While we're here, we simplify the relationship between
TryTransmuteFromPtr, MutationCompatible, and Read. In particular,
TryTransmuteFromPtr now has a single blanket impl which is bounded by
MutationCompatible, and MutationCompatible now directly supports
three cases (and no longer delegates to Read):

  • Exclusive reference transmutes in which the destination type may be
    transmuted into the source type
  • Shared reference transmutes in which both the source and the
    destination type are Immutable
  • Shared reference transmutes which permit interior mutation, in which
    the source and destination types may both be transmuted into one
    another, and in which the source and destination types support
    compatible interior mutation

Makes progress on #1817

Co-authored-by: Jack Wrenn [email protected]


This PR is on branch transmute-ref-dst.

@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch from 0dc4ac5 to 5f19ed9 Compare June 9, 2025 19:20
@joshlf joshlf force-pushed the I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b branch 2 times, most recently from e430a16 to 4abed50 Compare June 9, 2025 19:30
@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch from 5f19ed9 to df3a207 Compare June 9, 2025 19:30
@joshlf joshlf force-pushed the I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b branch from 4abed50 to 7e1bf34 Compare June 9, 2025 20:13
@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch 2 times, most recently from 15f9941 to 0d9185f Compare June 9, 2025 20:16
@joshlf joshlf force-pushed the I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b branch 2 times, most recently from d3aad92 to 9fda82b Compare June 9, 2025 23:11
@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch from 0d9185f to c18affd Compare June 9, 2025 23:11
@joshlf joshlf force-pushed the I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b branch from 9fda82b to eef2e15 Compare June 9, 2025 23:11
@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch from c18affd to 50bb997 Compare June 9, 2025 23:11
joshlf and others added 2 commits June 10, 2025 09:12
In `transmute!`, support an `#![allow(shrink)]` attribute which is
invoked as follows:

  transmute!(#![allow(shrink)] src);

When this attribute is provided, `transmute!` will permit shrinking
transmutes, in which the destination value may be smaller than the
source value.

Co-authored-by: Jack Wrenn <[email protected]>
gherrit-pr-id: I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1
Generalize `SizeEq`, renaming it to `SizeFrom`, and supporting casts
which shrink rather than preserve referent size.

This requires fairly deep surgery in our transmute taxonomy. We can no
longer rely on `SizeFrom::cast_from_raw` preserving referent size, which
means that all pointer transmutes must now reason about two types of
transmutes:
- Size-shrinking transmutes (in which the source value is larger than
  the destination value)
- Overwriting transmutes (in which the source value is larger than the
  destination value *and* the destination permits mutation). In an
  overwriting transmute, the resulting value is a concatenation of a
  valid `T` and the *suffix* of a valid `U`.

(Note that size-preserving transmutes can be viewed as a special case of
size-shrinking transmutes.)

In order to support these semantics, we split `TransmuteFrom` into two
traits: `TransmuteFrom` (which supports shriking transmutes) and
`TransmuteOverwrite` (which supports overwriting transmutes).

While we're here, we simplify the relationship between
`TryTransmuteFromPtr`, `MutationCompatible`, and `Read`. In particular,
`TryTransmuteFromPtr` now has a single blanket impl which is bounded by
`MutationCompatible`, and `MutationCompatible` now directly supports
three cases (and no longer delegates to `Read`):
- Exclusive reference transmutes in which the destination type may be
  transmuted into the source type
- Shared reference transmutes in which both the source and the
  destination type are `Immutable`
- Shared reference transmutes which permit interior mutation, in which
  the source and destination types may both be transmuted into one
  another, and in which the source and destination types support
  compatible interior mutation

Makes progress on #1817

Co-authored-by: Jack Wrenn <[email protected]>
gherrit-pr-id: I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b
@joshlf joshlf force-pushed the I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 branch from 50bb997 to 8e4a6e5 Compare June 10, 2025 13:12
@joshlf joshlf force-pushed the I6c793a9620ad75bdc0d26ab7c7cd1a0c7bef1b8b branch from eef2e15 to 3fc3abf Compare June 10, 2025 13:12
@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 96.61017% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.75%. Comparing base (8e4a6e5) to head (3fc3abf).

Files with missing lines Patch % Lines
src/lib.rs 92.30% 1 Missing ⚠️
src/util/macros.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@                              Coverage Diff                              @@
##           I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1    #2580      +/-   ##
=============================================================================
+ Coverage                                      88.72%   88.75%   +0.02%     
=============================================================================
  Files                                             20       20              
  Lines                                           5332     5335       +3     
=============================================================================
+ Hits                                            4731     4735       +4     
+ Misses                                           601      600       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Base automatically changed from I46b18b4b1d10507b7e1d2e01b09dc4960cfcdce1 to main June 10, 2025 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants