fn to_bytes(&self) -> Result<[u8; SIZE_OF_TYPE], Error> {
const {
if SIZE_OF_TYPE != size_of::<Self::BaseType>() {
panic!("incorrect size");
}
}
todo!("rest of the function")
}
But other than that, you are running into the fundamental limitations of current const generics: you can never have a calculation “in the middle” such that the value of a const generic depends on another generic indirectly; you can only use non-generic constant expressions, and copy other const generic parameters without doing any computation. Improving on that is tracked by Tracking Issue for complex generic constants: `feature(generic_const_exprs)` · Issue #76560 · rust-lang/rust · GitHub
I had a similar problem and came to the conclusion that it's easier to just return a Vec<u8>.
If you want to avoid the heap allocation, and if the data to be return is usually small, consider using ArrayVec (or maybe TinyVec) from the tinyvec create.