Last active
May 3, 2024 08:12
-
-
Save ydm/83ca5e9039c8cf722c5b6be3a48b3e02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(associated_type_defaults)] | |
#![feature(generic_const_exprs)] | |
#[derive(Debug)] | |
struct Vector<const D: usize> { | |
v: [f32; D], | |
} | |
impl<const D: usize> Vector<D> { | |
fn new() -> Self { | |
Vector::<D> { v: [0.0; D] } | |
} | |
fn x(&self) -> f32 { | |
self.v[0] | |
} | |
fn y(&self) -> f32 { | |
self.v[1] | |
} | |
fn z(&self) -> f32 { | |
self.v[2] | |
} | |
// fn w(&self) -> f32 { | |
// self.v[3] | |
// } | |
} | |
trait MatrixTrait { | |
const D: usize; | |
type V = Vector::<{Self::D}> where [();Self::D]: Sized; | |
} | |
struct Matrix2 {} | |
impl MatrixTrait for Matrix2 { | |
const D: usize = 2; | |
} | |
struct Matrix3 {} | |
impl MatrixTrait for Matrix3 { | |
const D: usize = 3; | |
} | |
fn main() { | |
let a = <Matrix2 as MatrixTrait>::V::new(); | |
println!("a.x={} a.y={}", a.x(), a.y()); | |
let b = <Matrix3 as MatrixTrait>::V::new(); | |
println!("b.x={} b.y={}, b.z={}", b.x(), b.y(), b.z()); | |
} |
Author
ydm
commented
May 3, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment