Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Last active October 8, 2020 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/66e74760f1537538de0ead7be90d4b71 to your computer and use it in GitHub Desktop.
Save derrickturk/66e74760f1537538de0ead7be90d4b71 to your computer and use it in GitHub Desktop.
We /lightweight dependent types/ now, again, maybe
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
fn append<T: Copy + Default, const N: usize, const M: usize>(
xs: &[T; N], ys: &[T; M]) -> [T; N + M] {
let mut result = [T::default(); N + M];
result[..N].copy_from_slice(&xs[..]);
result[N..].copy_from_slice(&ys[..]);
result
}
fn main() {
let arr = append(&[1i32; 3], &[2i32; 4]);
dbg!(arr);
}
#![feature(const_generics, const_evaluatable_checked)]
#![allow(incomplete_features)]
// tested with rustc 1.49.0-nightly (91a79fb29 2020-10-07)
#[inline]
const fn add_em(x: usize, y: usize) -> usize {
x + y
}
#[inline]
fn append<T: Copy + Default, const N: usize, const M: usize>(
xs: &[T; N], ys: &[T; M]) -> [T; add_em(N, M)] {
let mut result = [T::default(); add_em(N, M)];
result[..N].copy_from_slice(&xs[..]);
result[N..].copy_from_slice(&ys[..]);
result
}
fn main() {
let arr = append(&[1i32; 3], &[2i32; 4]);
dbg!(arr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment