Skip to content

Feature request: Custom destructuring #78154

Closed
@LoganDark

Description

@LoganDark

Let's say I have this struct, Vec2:

#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Vec2 {
	x: f32,
	y: f32
}

And I also a tuple containing two Vec2s:

let t: (Vec2, Vec2) = ...;

Right now, I have to do this to destructure it, which is really long and cumbersome to type:

let (Vec2 { x: mut x1, y: mut y1 }, Vec2 { x: mut x2, y: mut y2 }) = t;

I thought it would be a lot nicer if I could do this instead:

let ((mut x1, mut y1), (mut x2, mut y2)) = t;

But after asking for help in the Rust Discord, it looks like this is impossible!

Should support for custom destructuring like this be added? Is it even possible?

It could work like this:

impl Destructure<(f32, f32)> for Vec2 {
	fn destructure(self) -> (f32, f32) {
		(self.x, self.y)
	}
}

impl DestructureRef<(&f32, &f32)> for Vec2 {
	fn destructure_ref(&self) -> (&f32, &f32) {
		(self.x, self.y)
	}
}

impl DestructureMut<(&mut f32, &mut f32)> for Vec2 {
	fn destructure_mut(&mut self) -> (&mut f32, &mut f32) {
		(self.x, self.y)
	}
}

Or it could use the existing Into/AsRef/AsMut traits instead, thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions