Skip to content

Instantly share code, notes, and snippets.

@ydm
Last active May 3, 2024 08:12
Show Gist options
  • Save ydm/83ca5e9039c8cf722c5b6be3a48b3e02 to your computer and use it in GitHub Desktop.
Save ydm/83ca5e9039c8cf722c5b6be3a48b3e02 to your computer and use it in GitHub Desktop.
#![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());
}
@ydm
Copy link
Author

ydm commented May 3, 2024

$ cargo r
   Compiling hell v0.1.0 (/home/d/dev/rust/hell)
warning: the feature `generic_const_exprs` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/main.rs:2:12
  |
2 | #![feature(generic_const_exprs)]
  |            ^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #76560 <https://github.com/rust-lang/rust/issues/76560> for more information
  = note: `#[warn(incomplete_features)]` on by default

warning: `hell` (bin "hell") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
     Running `/home/d/dev/rust/hell/target/debug/hell`
a.x=0 a.y=0
b.x=0 b.y=0, b.z=0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment