Skip to content

Feature flag to add repr(c) to bevy types #14362

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ ios_simulator = ["bevy_internal/ios_simulator"]
# Enable built in global state machines
bevy_state = ["bevy_internal/bevy_state"]

# Enable C style struct representation to allow passing built-in types over FFI
repr_c = ["bevy_internal/repr_c"]

[dependencies]
bevy_internal = { path = "crates/bevy_internal", version = "0.15.0-dev", default-features = false }

Expand Down
7 changes: 7 additions & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ ios_simulator = ["bevy_pbr?/ios_simulator", "bevy_render?/ios_simulator"]
# Enable built in global state machines
bevy_state = ["dep:bevy_state"]

# Enable C style struct representation to allow passing built-in types over FFI
repr_c = [
"bevy_transform/repr_c",
"bevy_math/repr_c",
"bevy_reflect/repr_c"
]

[dependencies]
# bevy
bevy_a11y = { path = "../bevy_a11y", version = "0.15.0-dev" }
Expand Down
5 changes: 5 additions & 0 deletions crates/bevy_math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ debug_glam_assert = ["glam/debug-glam-assert"]
# Enable the rand dependency for shape_sampling
rand = ["dep:rand", "dep:rand_distr", "glam/rand"]

repr_c = [
"glam/scalar-math",
"bevy_reflect/repr_c"
]

[lints]
workspace = true

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/affine3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_reflect::Reflect;
/// significant performance impact. Convert to `glam::Affine3A` to do
/// non-trivial calculations.
#[cfg_attr(feature = "bevy_reflect", derive(Reflect))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Affine3 {
/// Scaling, rotation, shears, and other non-translation affine transforms
pub matrix3: Mat3,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use bevy_reflect::Reflect;
/// An `AspectRatio` is the ratio of width to height.
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug, PartialEq))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct AspectRatio(f32);

impl AspectRatio {
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/bounding/bounded2d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub trait Bounded2d {
#[doc(alias = "BoundingRectangle")]
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Aabb2d {
/// The minimum, conventionally bottom-left, point of the box
pub min: Vec2,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/bounding/bounded3d/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait Bounded3d {
/// A 3D axis-aligned bounding box
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Aabb3d {
/// The minimum point of the box
pub min: Vec3A,
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/src/bounding/raycast2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_reflect::Reflect;
/// A raycast intersection test for 2D bounding volumes
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct RayCast2d {
/// The ray for the test
pub ray: Ray2d,
Expand Down Expand Up @@ -105,6 +106,7 @@ impl IntersectsVolume<BoundingCircle> for RayCast2d {
/// An intersection test that casts an [`Aabb2d`] along a ray.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct AabbCast2d {
/// The ray along which to cast the bounding volume
pub ray: RayCast2d,
Expand Down Expand Up @@ -143,6 +145,7 @@ impl IntersectsVolume<Aabb2d> for AabbCast2d {
/// An intersection test that casts a [`BoundingCircle`] along a ray.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct BoundingCircleCast {
/// The ray along which to cast the bounding volume
pub ray: RayCast2d,
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/src/bounding/raycast3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy_reflect::Reflect;
/// A raycast intersection test for 3D bounding volumes
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct RayCast3d {
/// The origin of the ray.
pub origin: Vec3A,
Expand Down Expand Up @@ -100,6 +101,7 @@ impl IntersectsVolume<BoundingSphere> for RayCast3d {
/// An intersection test that casts an [`Aabb3d`] along a ray.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct AabbCast3d {
/// The ray along which to cast the bounding volume
pub ray: RayCast3d,
Expand Down Expand Up @@ -143,6 +145,7 @@ impl IntersectsVolume<Aabb3d> for AabbCast3d {
/// An intersection test that casts a [`BoundingSphere`] along a ray.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "bevy_reflect", derive(Reflect), reflect(Debug))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct BoundingSphereCast {
/// The ray along which to cast the bounding volume
pub ray: RayCast3d,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/compass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Deserialize, Serialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub enum CompassQuadrant {
/// Corresponds to [`Dir2::Y`] and [`Dir2::NORTH`]
North,
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_math/src/direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub type Direction3d = Dir3;
reflect(Serialize, Deserialize)
)]
#[doc(alias = "Direction2d")]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Dir2(Vec2);
impl Primitive2d for Dir2 {}

Expand Down Expand Up @@ -345,6 +346,7 @@ impl approx::UlpsEq for Dir2 {
reflect(Serialize, Deserialize)
)]
#[doc(alias = "Direction3d")]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Dir3(Vec3);
impl Primitive3d for Dir3 {}

Expand Down Expand Up @@ -551,6 +553,7 @@ impl approx::UlpsEq for Dir3 {
reflect(Serialize, Deserialize)
)]
#[doc(alias = "Direction3dA")]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Dir3A(Vec3A);
impl Primitive3d for Dir3A {}

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/float_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use bevy_reflect::Reflect;
derive(Reflect),
reflect(Debug, PartialEq, Hash)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct FloatOrd(pub f32);

impl PartialOrd for FloatOrd {
Expand Down
5 changes: 4 additions & 1 deletion crates/bevy_math/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ mod rects;
mod rotation2d;
#[cfg(feature = "rand")]
pub mod sampling;

// mod quat;

pub use compass::{CompassOctant, CompassQuadrant};

pub use affine3::*;
Expand Down Expand Up @@ -58,4 +61,4 @@ pub mod prelude {
};
}

pub use glam::*;
pub use glam::*;
4 changes: 4 additions & 0 deletions crates/bevy_math/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Circle {
/// The radius of the circle
pub radius: f32,
Expand Down Expand Up @@ -107,6 +108,7 @@ impl Measured2d for Circle {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Arc2d {
/// The radius of the circle
pub radius: f32,
Expand Down Expand Up @@ -274,6 +276,7 @@ impl Arc2d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct CircularSector {
/// The arc defining the sector
#[cfg_attr(feature = "serialize", serde(flatten))]
Expand Down Expand Up @@ -413,6 +416,7 @@ impl CircularSector {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct CircularSegment {
/// The arc defining the segment
#[cfg_attr(feature = "serialize", serde(flatten))]
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_math/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Sphere {
/// The radius of the sphere
pub radius: f32,
Expand Down Expand Up @@ -92,6 +93,7 @@ impl Measured3d for Sphere {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Plane3d {
/// The normal of the plane. The plane will be placed perpendicular to this direction
pub normal: Dir3,
Expand Down Expand Up @@ -164,6 +166,7 @@ impl Plane3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct InfinitePlane3d {
/// The normal of the plane. The plane will be placed perpendicular to this direction
pub normal: Dir3,
Expand Down Expand Up @@ -226,6 +229,7 @@ impl InfinitePlane3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Line3d {
/// The direction of the line
pub direction: Dir3,
Expand All @@ -241,6 +245,7 @@ impl Primitive3d for Line3d {}
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Segment3d {
/// The direction of the line
pub direction: Dir3,
Expand Down Expand Up @@ -300,6 +305,7 @@ impl Segment3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Polyline3d<const N: usize> {
/// The vertices of the polyline
#[cfg_attr(feature = "serialize", serde(with = "super::serde::array"))]
Expand Down Expand Up @@ -331,6 +337,7 @@ impl<const N: usize> Polyline3d<N> {
/// For a version without alloc: [`Polyline3d`]
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct BoxedPolyline3d {
/// The vertices of the polyline
pub vertices: Box<[Vec3]>,
Expand Down Expand Up @@ -366,6 +373,7 @@ impl BoxedPolyline3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Cuboid {
/// Half of the width, height and depth of the cuboid
pub half_size: Vec3,
Expand Down Expand Up @@ -458,6 +466,7 @@ impl Measured3d for Cuboid {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Cylinder {
/// The radius of the cylinder
pub radius: f32,
Expand Down Expand Up @@ -536,6 +545,7 @@ impl Measured3d for Cylinder {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Capsule3d {
/// The radius of the capsule
pub radius: f32,
Expand Down Expand Up @@ -606,6 +616,7 @@ impl Measured3d for Capsule3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Cone {
/// The radius of the base
pub radius: f32,
Expand Down Expand Up @@ -684,6 +695,7 @@ impl Measured3d for Cone {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct ConicalFrustum {
/// The radius of the top of the frustum
pub radius_top: f32,
Expand Down Expand Up @@ -736,6 +748,7 @@ pub enum TorusKind {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Torus {
/// The radius of the tube of the torus
#[doc(
Expand Down Expand Up @@ -847,6 +860,7 @@ impl Measured3d for Torus {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Triangle3d {
/// The vertices of the triangle.
pub vertices: [Vec3; 3],
Expand Down Expand Up @@ -1040,6 +1054,7 @@ impl Measured2d for Triangle3d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Tetrahedron {
/// The vertices of the tetrahedron.
pub vertices: [Vec3; 4],
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/primitives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub trait Primitive3d {}
/// The winding order for a set of points
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[doc(alias = "Orientation")]
#[cfg_attr(feature = "repr_c", repr(C))]
pub enum WindingOrder {
/// A clockwise winding order
Clockwise,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_math/src/ray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Deserialize, Serialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Ray2d {
/// The origin of the ray.
pub origin: Vec2,
Expand Down Expand Up @@ -65,6 +66,7 @@ impl Ray2d {
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Deserialize, Serialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Ray3d {
/// The origin of the ray.
pub origin: Vec3,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/rects/irect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct IRect {
/// The minimum corner point of the rect.
pub min: IVec2,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/rects/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Rect {
/// The minimum corner point of the rect.
pub min: Vec2,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/rects/urect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
all(feature = "serialize", feature = "bevy_reflect"),
reflect(Serialize, Deserialize)
)]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct URect {
/// The minimum corner point of the rect.
pub min: UVec2,
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_math/src/rotation2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use bevy_reflect::{ReflectDeserialize, ReflectSerialize};
reflect(Serialize, Deserialize)
)]
#[doc(alias = "rotation", alias = "rotation2d", alias = "rotation_2d")]
#[cfg_attr(feature = "repr_c", repr(C))]
pub struct Rot2 {
/// The cosine of the rotation angle in radians.
///
Expand Down
Loading