Skip to content
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

Allow From/Into conversions of Point and tuple/arrays of f64 #32

Merged
merged 1 commit into from
Oct 23, 2023

Conversation

Chris--B
Copy link
Contributor

@Chris--B Chris--B commented Oct 23, 2023

This is a common pattern that I like to use for various linear algebra work in Rust:

let points: &[Some2DPoint] = &[
   (0., 1.).into(),
   (1., 0.).into(),
   (1., 1.).into(),
];

Subjectively, it's cleaner. It also helps with interop across math frameworks, since many of them can round trip through tuples or arrays. In C, these types are often unions anyway.

This change adds impls for From (and by extension Into) to allow round tripping Point, (f64, 64), and [f64; 2]. It's a small change with a simple API expansion and simple implementation + tests. It does not add any traits for &[f64], those will still have to be copied into one of the above types explicitly.

I make use of this so maybe someone else will benefit here. :)

There's a lot of 2d vector/point types, and things like this simplify
interop.

Note: This is only fixed sized, 2-length arrays. &[f64] is not covered
and would need to be cast/copied into a [f64; 2] to work.
@Chris--B Chris--B marked this pull request as ready for review October 23, 2023 02:19
@mourner
Copy link
Owner

mourner commented Oct 23, 2023

Looks good to me, thanks for the contribution!

@mourner mourner merged commit fd14611 into mourner:master Oct 23, 2023
3 checks passed
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