]> git.lizzy.rs Git - generate-random.git/commitdiff
Add support for cgmath and collision main
authorLizzy Fleckenstein <eliasfleckenstein@web.de>
Tue, 9 May 2023 16:26:12 +0000 (18:26 +0200)
committerLizzy Fleckenstein <eliasfleckenstein@web.de>
Tue, 9 May 2023 16:26:12 +0000 (18:26 +0200)
lib/Cargo.toml
lib/src/lib.rs

index ddd300b93311ed93f181bafe8914562993cf7708..1513795251917fd9b8f132fff293628601ee6efd 100644 (file)
@@ -11,10 +11,16 @@ description = "Generate random data"
 readme = "README.md"
 keywords = ["random"]
 
+[features]
+cgmath = ["dep:cgmath"]
+collision = ["cgmath", "dep:collision"]
+
 [dependencies]
 rand = "0.8.5"
 generate-random-macro = { version = "0.1.0", path = "../derive-macro" }
 enumset = { git = "https://github.com/Lymia/enumset", optional = true }
+cgmath = { version = "0.17.0", optional = true }
+collision = { version = "0.20.1", optional = true }
 
 [dev-dependencies]
 rand_chacha = "0.3.1"
index bd49e6be21f52d9f76ef9a9791582ab3e634f453..71f68a898a386aadce0547bcbc1d07aaa73384b3 100644 (file)
@@ -226,6 +226,79 @@ macro_rules! impl_generate_random_tuple {
 
 impl_generate_random_tuple!(A B C D E F G H I J K L);
 
+#[cfg(feature = "cgmath")]
+mod impl_cgmath {
+    use super::*;
+    use cgmath::*;
+
+    macro_rules! impl_from {
+               ($ident:ident, $from:ty, $bound:ident $(+ $others:ident )*) => {
+                       impl<T: $bound $(+ $others)* + GenerateRandom> GenerateRandom for $ident<T> {
+                               fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
+                                       <$from>::generate_random(rng).into()
+                               }
+                       }
+               };
+       }
+
+    macro_rules! impl_wrap {
+        ($ident:ident) => {
+            impl<T: GenerateRandom> GenerateRandom for $ident<T> {
+                fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
+                    Self(GenerateRandom::generate_random(rng))
+                }
+            }
+        };
+    }
+
+    impl_from!(Vector1, [T; 1], Clone);
+    impl_from!(Vector2, [T; 2], Clone);
+    impl_from!(Vector3, [T; 3], Clone);
+    impl_from!(Vector4, [T; 4], Clone);
+
+    impl_from!(Point1, [T; 1], Clone);
+    impl_from!(Point2, [T; 2], Clone);
+    impl_from!(Point3, [T; 3], Clone);
+
+    impl_from!(Matrix2, [[T; 2]; 2], Copy);
+    impl_from!(Matrix3, [[T; 3]; 3], Copy);
+    impl_from!(Matrix4, [[T; 4]; 4], Copy);
+
+    impl_wrap!(Deg);
+    impl_wrap!(Rad);
+
+    impl<A: GenerateRandom> GenerateRandom for Euler<A> {
+        fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
+            let x = GenerateRandom::generate_random(rng);
+            let y = GenerateRandom::generate_random(rng);
+            let z = GenerateRandom::generate_random(rng);
+            Self::new(x, y, z)
+        }
+    }
+}
+
+#[cfg(feature = "collision")]
+mod impl_collision {
+    use super::*;
+    use cgmath::*;
+    use collision::*;
+
+    macro_rules! impl_abbb {
+        ($ident:ident) => {
+            impl<S: GenerateRandom + BaseNum> GenerateRandom for $ident<S> {
+                fn generate_random<R: rand::Rng + ?Sized>(rng: &mut R) -> Self {
+                    let p1 = GenerateRandom::generate_random(rng);
+                    let p2 = GenerateRandom::generate_random(rng);
+                    Self::new(p1, p2)
+                }
+            }
+        };
+    }
+
+    impl_abbb!(Aabb2);
+    impl_abbb!(Aabb3);
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;