Stream: general

Topic: waiting for const generics


Jake Goulding (Jun 23 2019 at 19:59, on Zulip):

Preaching to the choir here, but I’m excited for const generics.

Jake Goulding (Jun 30 2019 at 00:28, on Zulip):

Can something like this be done with a number of feature flags today?

#![feature(const_generics)]

use std::mem;

struct BplusTreeLeaf<K, V, const PAGE_SIZE: usize> {
    keys: [K; PAGE_SIZE / (mem::size_of::<K>() + mem::size_of::<V>())],
    values: [V; PAGE_SIZE / (mem::size_of::<K>() + mem::size_of::<V>())],
}
Jake Goulding (Jun 30 2019 at 00:32, on Zulip):

Even better would be

#![feature(const_generics)]

use std::mem;

struct BplusTreeLeaf<K, V, const PAGE_SIZE: usize> {
    keys: [K; Self::CAPACITY],
    values: [V; Self::CAPACITY],
}

impl<K, V, const PAGE_SIZE: usize> BplusTreeLeaf<K, V, {PAGE_SIZE}>{
    const CAPACITY: usize = PAGE_SIZE / (mem::size_of::<K>() + mem::size_of::<V>());
}

But I know associated consts and array lengths are not the best of friends

Last update: Nov 21 2019 at 23:55UTC