Skip to content

Add extensible visitor support mutable partial variants#135

Merged
soareschen merged 8 commits intomainfrom
partial-mut-variants
Aug 5, 2025
Merged

Add extensible visitor support mutable partial variants#135
soareschen merged 8 commits intomainfrom
partial-mut-variants

Conversation

@soareschen
Copy link
Collaborator

@soareschen soareschen commented Aug 5, 2025

Summary

This PR adds support for using extensible visitors with mutable partial variants. With this, we can get a &mut reference of an enum, and perform visitor dispatch with mutable access to each variant.

In order to support this, a new HasExtractorMut trait has been added, to extract mutable variant fields. A new MapTypeRef trait is also added to map a T into either &'a T or &'a mut T depending on whether IsRef or IsMut is used.

Example

Consider a trait like:

pub trait CanScale {
    fn scale(&mut self, factor: f64);
}

We can now use a new visitor dispatcher MatchFirstWithValueHandlersMut to dispatch on a mutable reference of a shape:

impl CanScale for Shape {
    fn scale(&mut self, factor: f64) {
        MatchFirstWithValueHandlersMut::<Scale>::compute(&(), NoCode, (self, factor))
    }
}

@soareschen soareschen merged commit a024781 into main Aug 5, 2025
5 checks passed
@soareschen soareschen deleted the partial-mut-variants branch August 5, 2025 19:31
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