I would like to specify (somehow), that there is a table
that accepts numbers from some power of 2 up to another power of 2,
e.g. 8 -> 8192 which is 3 -> 13 in log2.
Is it possible to specify the Log2Lookup table such that I can put a type
on get() which will only accept values from 8 -> 8192, otherwise a compile error?
The job of the table is essentially to round memory allocation amounts up to the next
power of 2 and return some object from a table accordingly.
```rust
const std = @import("std");
// The export was just for looking in godbolt, not relevant.
export fn foo(x: usize) usize {
const table = Log2Lookup(u64, 3, 13){
.lookup = .{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
};
return table.get(x);
}
fn Log2Lookup(
comptime T: type,
comptime log2lower: u8,
comptime log2upper: u8,
) type {
const n_entries = log2upper - log2lower + 1;
return struct {
const Self = @This();
lookup: [n_entries]T,
fn get(self: *const Self, size: usize) T {
// Would need to runtime check here that size is suitable, (log2_int_ceil(size) <= log2upper)
const idx = std.math.log2_int_ceil(usize, size) - log2lower;
return self.lookup[idx];
}
};
}
```