You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo uses const generics to implement "presets" from the consensus-specs.
However, there are many of them making the code hard to read, extend, and maintain.
Here's a sample refactoring we could consider instead:
// in ssz primitivespubmod ssz_rs {pubtraitBounded{constVALUE:usize;}#[derive(Debug)]pubstructBound<constVALUE:usize>;impl<constBOUND:usize>BoundedforBound<BOUND>{constVALUE:usize = BOUND;}#[derive(Debug)]pubstructVector<T,B:Bounded>{pubdata:Vec<T>,_p: std::marker::PhantomData<B>,}impl<T,B:Bounded>Vector<T,B>{pubfnnew(data:Vec<T>) -> Self{ifB::VALUE > data.len(){panic!("not in bounds")}else{Self{
data,_p: std::marker::PhantomData,}}}}}// in ethereum-consensuspubmod consensus {usesuper::ssz_rs::*;pubtraitPreset{typeValidatorsBound:Bounded;}pubmod presets {usesuper::*;constMAINNET_VALIDATORS_BOUND:usize = 3;#[derive(Debug)]pubstructSomePreset<constBAR:usize>;impl<constBAR:usize>PresetforSomePreset<BAR>{typeValidatorsBound = Bound<BAR>;}pubtypeMainnet = SomePreset<MAINNET_VALIDATORS_BOUND>;}#[derive(Debug)]pubstructState<P:Preset>{pubvalidators:Vector<u8,P::ValidatorsBound>,pubb:usize,}pubfndo_state<P:Preset>(state:&mutState<P>){dbg!(state.validators.data.len());
state.b += 1;}}fnmain(){use consensus::{do_state, presets::Mainnet,State};use ssz_rs::Vector;letmut state = State::<Mainnet>{validators:Vector::new([23u8,14u8,32u8].to_vec()),b:234,};dbg!(&state);do_state(&mut state);dbg!(state);}
This approach uses another step of "type indirection" to pass the const bound of the SSZ type from the consumer's definition to the underlying target (bounded) type.
This would require some changes to ssz-rs, and make that code a bit more complex with the new Bounded abstraction; however, the consumer code replaces a long list of const generics with a single generic with bound Preset to group them all behind a single trait. As far as I can tell, this "indirection" through Bounded is required to avoid using the const value until the very end (use earlier is currently not supported by the Rust compiler...).
The upside is all of the types in this repo, and the functions that operate on them, become much simpler.
The text was updated successfully, but these errors were encountered:
This repo uses const generics to implement "presets" from the
consensus-specs
.However, there are many of them making the code hard to read, extend, and maintain.
Here's a sample refactoring we could consider instead:
This approach uses another step of "type indirection" to pass the const bound of the SSZ type from the consumer's definition to the underlying target (bounded) type.
This would require some changes to
ssz-rs
, and make that code a bit more complex with the newBounded
abstraction; however, the consumer code replaces a long list of const generics with a single generic with boundPreset
to group them all behind a single trait. As far as I can tell, this "indirection" throughBounded
is required to avoid using the const value until the very end (use earlier is currently not supported by the Rust compiler...).The upside is all of the types in this repo, and the functions that operate on them, become much simpler.
The text was updated successfully, but these errors were encountered: