Skip to content

Allow customization of key sizes #52

@orlp

Description

@orlp

An investigation is necessary to see if we can safely allow customization of key sizes. Something along the following lines should work:

pub trait Key {
    type Index: KeyIndex;
    type Version: KeyVersion;
}

pub trait KeyIndex: private::Sealed {
    #[doc(hidden)]
    type T: UInt;
}

pub trait KeyVersion: private::Sealed {
    #[doc(hidden)]
    type T: UInt;
    #[doc(hidden)]
    type NonZeroT: NonZeroUInt;
}

// All the ops needed for implementing.
#[doc(hidden)]
pub trait NonZeroUInt: Copy + PartialEq + Eq /* ... */ {}
#[doc(hidden)]
pub trait UInt: Copy + PartialEq + Eq /* ... */  {}

mod private {
    pub trait Sealed {}
}

// Implement all for u8, u16, u32, u64, u128...
impl UInt for u32 {}
impl NonZeroUInt for NonZeroU32 {}
impl KeyIndex for u32 {
    type T = u32;
}
impl KeyVersion for u32 {
    type T = u32;
    type NonZeroT = NonZeroU32;
}

impl private::Sealed for u32 {}


pub struct KeyData<I: KeyIndex, V: KeyVersion> {
    idx: I::T,
    version: V::NonZeroT,
}

// Allow following syntax:
new_key_type! {
    struct DefaultKey;             // u32 index/version
    struct SmallKey(u16, u16);     // u16 index/version
    struct HardenedKey(u32, u128); // u32 index, u128 version
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions