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

Implement From trait for cpVect ⟷ tuple and array #5

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jacius
Copy link

@jacius jacius commented Sep 11, 2016

I have implemented the standard From trait for converting a cpVect or &cpVect into a (cpFloat, cpFloat) tuple or [cpFloat; 2] array, which are the same as (f64, f64) and [f64; 2]. I have also implemented the trait for the opposite direction, converting a tuple or array into a cpVect. This makes it very easy to convert back and forth, which can simplify a lot of code in the chipmunk crate, or anyone who uses the chipmunk-sys crate directly.

Implementing the From trait also gives us the Into trait automatically.

This makes the cpVect::to_tuple() function obsolete. You can write v.into() or <(f64, f64)>::from(v) instead of v.to_tuple(). I have marked cpVect::to_tuple() as deprecated. It doesn't exist in any released version of this crate, but it is used by the master branch of the chipmunk crate, so I didn't want to remove it completely yet.

This pull request is based on the commits in pull request #2. I recommend merging that pull request first.

The following functions do not perform any mutation, so they can take
const pointers instead of mut pointers:

- cpArbiterGetSurfaceVelocity
- cpBodyGetType
- cpShapeGetArea
- cpShapeGetCenterOfGravity
- cpSpaceContainsShape
- cpSpaceContainsBody
- cpSpaceContainsConstraint
- cpArrayContains

This allows the bindings to be called with an immutable reference.

The following functions were also considered, but they perform some
internal mutation (locking), so the bindings were not changed:

- cpSpacePointQuery
- cpSpacePointQueryNearest
- cpSpaceSegmentQuery
- cpSpaceSegmentQueryFirst
- cpSpaceBBQuery
- cpSpaceShapeQuery
Cargo does not use the package.lib key, and it is not necessary
because Cargo compiles the crate as chipmunk_sys anyway.
The naming style is intended to match the C library, so it is okay
not to follow Rust naming conventions.
- (cpFloat, cpFloat) can be created from cpVect or &cpVect.
- cpVect can be created from (cpFloat, cpFloat).

Implementing From automatically gives us Into for free.

cpVect::to_tuple() is now deprecated.
Use `v.into()` or `From::from(v)` instead.

cpVect::to_tuple() never existed in a release of this crate, but the
master branch of the chipmunk crate uses it, so I deprecated it
instead of simply removing it.
- [cpFloat; 2] can be created from cpVect or &cpVect.
- cpVect can be created from [cpFloat; 2].

This gives users more flexibility. For example, the nalbegra crate
has a Vector2 type which can easily be created from [f64; 2], but
does not support (f64, f64).
@jacius jacius changed the title Implement From trait for cpVect ⟷ tuple Implement From trait for cpVect ⟷ tuple and array Sep 13, 2016
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