29 lines
1.5 KiB
Plaintext
29 lines
1.5 KiB
Plaintext
import std::io;
|
|
|
|
# A constant a trait asks for, read through a type parameter it bounds, is a
|
|
# constant wherever one stands: a fixed array's length, in a signature or a
|
|
# field too, a constant type argument, or a `const`, each instance giving it
|
|
# the type's own. So are the constants of `array` and `pointer`, and a local
|
|
# `const` worked out from a constant parameter.
|
|
trait sized { const bytes: u64; }
|
|
const i32::bytes: u64 = 4;
|
|
const i64::bytes: u64 = 8;
|
|
struct buffer<T, const N: u64> { var items: T[N]; }
|
|
struct holder<T> where T: sized { var raw: u8[T::bytes]; var held: buffer<u8, T::bytes * 2>; }
|
|
|
|
def flag<const B: bool>(): u64 { return B ? 1u : 0u; }
|
|
def count<const N: u64>(): u64 { return N; }
|
|
|
|
def raw<T>(x: T): u8[T::bytes] where T: sized { var out: u8[T::bytes]; return out; }
|
|
def padded<T>(x: T): buffer<u8, T::bytes + 1> where T: sized { var out: buffer<u8, T::bytes + 1>; return out; }
|
|
def local<T>(x: T): u64 where T: sized { const n: u64 = T::bytes * 3; var arr: u8[n]; return arr.length + count::<n>(); }
|
|
def doubled<T>(): u64 where T: array { return size_of::<u8[T::length * 2]>(); }
|
|
def writes<T>(): u64 where T: pointer { return flag::<T::mutable>(); }
|
|
def scaled<const N: u64>(): u64 { const m: u64 = N + 1; var arr: u8[m]; return arr.length; }
|
|
|
|
def main(): void {
|
|
var h: holder<i64>;
|
|
println($"{raw(3 as i32).length} {raw(3).length} {padded(3 as i32).items.length} {h.raw.length} {h.held.items.length}");
|
|
println($"{local(3 as i32)} {local(3)} {doubled::<u16[5]>()} {writes::<*mut u8>()} {writes::<*u8>()} {scaled::<4>()}");
|
|
}
|