Skip to content

Implement AsView and AsMut traits for Grpc messages - #1

Open
sauravzg wants to merge 1 commit into
masterfrom
grpc_message_traits
Open

Implement AsView and AsMut traits for Grpc messages#1
sauravzg wants to merge 1 commit into
masterfrom
grpc_message_traits

Conversation

@sauravzg

Copy link
Copy Markdown
Owner

No description provided.

@sauravzg

Copy link
Copy Markdown
Owner Author

These traits are supposed to provide abstractions over standard references and protobuf views and muts to eliminate hard dependency on protobuf

The biggest challenge in the message module is to solve the long-standing difficulty of reborrowing mutable message views.

The Problem: Reborrowing GATs
When working with Generic Associated Types (GATs) for mutable views (e.g., type Mut<'a>), we frequently need to "reborrow" a view to pass it to a child function without consuming the original. However, traditional approaches fail here:
1. Foreign Types: We cannot add a .reborrow() method directly to protobuf::Mut as it is a foreign type.
2. HRTB Hell: Defining a separate Reborrow trait and requiring for<'a> T::Mut<'a>: Reborrow leads to complex Higher-Ranked Trait Bound errors (e.g., "implementation is not general enough") that are extremely difficult to satisfy in Rust's current type system.
The Solution: Static Operator Pattern
We bypass these issues by moving the reborrowing logic into the provider trait itself as a static operator.
* Introduced AsMut::reborrow_view<'a, 'b>(view: &'b mut Self::Mut<'a>) -> Self::Mut<'b>.
* This bundles the reborrowing logic with the type definition, avoiding the need for extra traits or complex HRTB bounds on the view type itself.
Changes:
* as_mut.rs: Defined AsMut with the reborrow_view static operator. Implemented it for protobuf::Message and added tests verifying that views can be reborrowed and mutated without consuming the original.
* as_view.rs: Defined AsView for immutable views.
@sauravzg
sauravzg force-pushed the grpc_message_traits branch from 67ec67a to 65ffdf3 Compare March 6, 2026 15:15
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.

1 participant