Skip to content

Conversation

@joshlf
Copy link
Member

@joshlf joshlf commented Dec 17, 2025

Add unsafe trait Project and unsafe trait Cast: Project. Cast is
implemented for any address-preserving cast, while Project generalizes
to conversions which may not preserve the address of the referent (ie,
field projections). Use these (mostly Cast, but some Project) to
unify PtrInner/Ptr casts, field projections, and SizeEq casts.
Replace a good amount of unsafe derive-generated code with uses of this
machinery.

Makes progress on #196
Closes #2856


Latest Update: v54 — Compare vs v53

📚 Full Patch History

Links show the diff between the row version and the column version.

Version Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50 v51 v52 v53
v54 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50 v51 v52 v53
v53 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50 v51 v52
v52 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50 v51
v51 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49 v50
v50 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48 v49
v49 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47 v48
v48 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46 v47
v47 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45 v46
v46 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44 v45
v45 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43 v44
v44 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42 v43
v43 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41 v42
v42 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40 v41
v41 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39 v40
v40 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38 v39
v39 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37 v38
v38 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36 v37
v37 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35 v36
v36 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34 v35
v35 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33 v34
v34 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32 v33
v33 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31 v32
v32 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30 v31
v31 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29 v30
v30 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28 v29
v29 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27 v28
v28 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26 v27
v27 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25 v26
v26 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24 v25
v25 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23 v24
v24 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22 v23
v23 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21 v22
v22 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20 v21
v21 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19 v20
v20 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18 v19
v19 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17 v18
v18 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16 v17
v17 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15 v16
v16 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14 v15
v15 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13 v14
v14 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12 v13
v13 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11 v12
v12 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10 v11
v11 Base v1 v2 v3 v4 v5 v6 v7 v8 v9 v10
v10 Base v1 v2 v3 v4 v5 v6 v7 v8 v9
v9 Base v1 v2 v3 v4 v5 v6 v7 v8
v8 Base v1 v2 v3 v4 v5 v6 v7
v7 Base v1 v2 v3 v4 v5 v6
v6 Base v1 v2 v3 v4 v5
v5 Base v1 v2 v3 v4
v4 Base v1 v2 v3
v3 Base v1 v2
v2 Base v1
v1 Base

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @joshlf, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the pointer module by introducing a new helper macro, project_ptr_inner!. This macro provides a safe and controlled way to project an existing PtrInner to a specific field within its underlying struct or union, with the added capability of performing type transmutes. The change aims to facilitate more flexible and safe low-level pointer operations while adhering to strict provenance and memory safety rules, backed by thorough testing.

Highlights

  • New project_ptr_inner! Macro: Introduces a new #[macro_export] macro designed to project a PtrInner to a specific field of its referent type. This macro allows for safe low-level pointer manipulation, enabling access to struct or union fields while adhering to strict safety requirements.
  • Optional Type Transmutation: The project_ptr_inner! macro supports an optional type annotation for the target field, allowing for type transmutes. This feature is carefully designed with safety requirements to ensure that the transmute preserves or shrinks the size of the referent, maintaining memory safety and correct provenance.
  • Comprehensive Test Coverage: Extensive unit tests have been added to validate the project_ptr_inner! macro's functionality across various scenarios, including sized fields, dynamically sized types (DSTs), zero-sized types (ZSTs), and projections within unions, ensuring robustness and correctness of the implementation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a project_ptr_inner! macro for projecting PtrInner to a field, along with a comprehensive test suite. The macro implementation appears sound, but I've found a few issues. There are a couple of TODO comments that should be FIXMEs according to the repository's style guide. More importantly, one of the new tests appears to rely on a pointer cast (*mut [u8] to *mut [()]) that is documented as a compile-time error in Rust, which could lead to undefined behavior or compiler-specific dependencies.

@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from 8a4e49b to 09d0afd Compare December 17, 2025 20:48
@codecov-commenter
Copy link

codecov-commenter commented Dec 17, 2025

Codecov Report

❌ Patch coverage is 71.55172% with 33 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.90%. Comparing base (b99b86d) to head (f201bec).

Files with missing lines Patch % Lines
src/layout.rs 25.00% 30 Missing ⚠️
src/impls.rs 0.00% 2 Missing ⚠️
src/pointer/mod.rs 96.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2854      +/-   ##
==========================================
- Coverage   91.94%   91.90%   -0.05%     
==========================================
  Files          20       20              
  Lines        5824     5778      -46     
==========================================
- Hits         5355     5310      -45     
+ Misses        469      468       -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.

@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch 9 times, most recently from 6450ea0 to 39e9cfb Compare December 19, 2025 20:43
@joshlf joshlf changed the base branch from main to G9e7039c715e1ec53e6d860909bb0d618898fd46b December 19, 2025 20:43
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch 4 times, most recently from db34e90 to 9a8da36 Compare December 19, 2025 20:50
@joshlf joshlf changed the title [pointer] Add project_ptr_inner! helper [pointer] Add generic Cast framework Dec 19, 2025
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch 3 times, most recently from 7da501e to e199498 Compare December 20, 2025 03:36
@jswrenn jswrenn force-pushed the G9e7039c715e1ec53e6d860909bb0d618898fd46b branch from 4e3e874 to 898e717 Compare December 20, 2025 17:06
@joshlf joshlf force-pushed the G9e7039c715e1ec53e6d860909bb0d618898fd46b branch from 898e717 to e28215f Compare December 20, 2025 18:50
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from e199498 to c37397b Compare December 20, 2025 18:50
@joshlf joshlf force-pushed the G9e7039c715e1ec53e6d860909bb0d618898fd46b branch from e28215f to 122da00 Compare December 20, 2025 19:04
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from c37397b to 21652c0 Compare December 20, 2025 19:04
Base automatically changed from G9e7039c715e1ec53e6d860909bb0d618898fd46b to main December 20, 2025 19:41
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from 28bb18f to 68e740c Compare December 22, 2025 04:57
@joshlf joshlf force-pushed the G03596047b3e8a9931799295d1e707540561c4e46 branch from eb73d5a to dd41efa Compare December 22, 2025 04:57
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from 68e740c to 1cdac73 Compare December 22, 2025 16:16
@joshlf joshlf force-pushed the G03596047b3e8a9931799295d1e707540561c4e46 branch 2 times, most recently from 5d01996 to 86b13be Compare December 22, 2025 16:27
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from 1cdac73 to 1882a99 Compare December 22, 2025 16:27
@joshlf joshlf force-pushed the G03596047b3e8a9931799295d1e707540561c4e46 branch from 86b13be to fd4aaf7 Compare December 22, 2025 16:34
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch 2 times, most recently from b66acbc to e01b45d Compare December 22, 2025 16:39
@joshlf joshlf force-pushed the G03596047b3e8a9931799295d1e707540561c4e46 branch 2 times, most recently from 28cf083 to 1c5cebf Compare December 22, 2025 19:27
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from e01b45d to fc0665c Compare December 22, 2025 19:27
Base automatically changed from G03596047b3e8a9931799295d1e707540561c4e46 to main December 22, 2025 20:06
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch 11 times, most recently from ed795f7 to c5bd760 Compare December 23, 2025 14:56
Add `unsafe trait Project` and `unsafe trait Cast: Project`. `Cast` is
implemented for any address-preserving cast, while `Project` generalizes
to conversions which may not preserve the address of the referent (ie,
field projections). Use these (mostly `Cast`, but some `Project`) to
unify `PtrInner`/`Ptr` casts, field projections, and `SizeEq` casts.
Replace a good amount of unsafe derive-generated code with uses of this
machinery.

Makes progress on #196
Closes #2856

gherrit-pr-id: G57b42ad4ff767a765f9b4ac149aa1c19d3796711
@joshlf joshlf force-pushed the G57b42ad4ff767a765f9b4ac149aa1c19d3796711 branch from c5bd760 to f201bec Compare December 23, 2025 15:01
@joshlf
Copy link
Member Author

joshlf commented Dec 23, 2025

Replaced by #2860

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.

Support HasField-based projection in Ptr

2 participants