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

Make euclid types repr(C). #176

Merged
merged 1 commit into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::fmt;
/// another. See the `ScaleFactor` docs for an example.
// Uncomment the derive, and remove the macro call, once heapsize gets
// PhantomData<T> support.
#[repr(C)]
#[derive(RustcDecodable, RustcEncodable)]
pub struct Length<T, Unit>(pub T, PhantomData<Unit>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is safe, even though rustc will complain about it until rust-lang/rust#39462 lands.

Also, note that depending of the size of T, being Length a struct but T an integer/floating point value, passing Length as an argument instead of T may break the ABI in 32bit linux, etc, see the last comments in rust-lang/rfcs#1758.

I don't think you're going to pass Length directly through, right?


Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
//! Client code typically creates a set of aliases for each type and doesn't need
//! to deal with the specifics of typed units further. For example:
//!
//! All euclid types are marked #[repr(C)] in order to facilitate exposing them to
//! foreign function interfaces (provided the underlying scalar type is also repr(C)).
//!
//! ```rust
//! use euclid::*;
//! pub struct ScreenSpace;
Expand Down
1 change: 1 addition & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ macro_rules! define_matrix {
$(pub $field:ident: T,)+
}
) => (
#[repr(C)]
$(#[$attr])*
pub struct $name<T, $($phantom),+> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto regarding phantomdata.

$(pub $field: T,)+
Expand Down
1 change: 1 addition & 0 deletions src/scale_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::marker::PhantomData;
/// let one_foot: Length<f32, Inch> = Length::new(12.0);
/// let one_foot_in_mm: Length<f32, Mm> = one_foot * mm_per_inch;
/// ```
#[repr(C)]
#[derive(RustcDecodable, RustcEncodable)]
pub struct ScaleFactor<T, Src, Dst>(pub T, PhantomData<(Src, Dst)>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And ditto again, similarly to Length.


Expand Down