Hi all,
I writing an algorithm that can adapt itself to differently-sized integers with the help of num-traits
. The code needs to determine the size of a bounded generic type compile-time. However, when I compiled the code, I got complaints about error[E0401]: can't use generic parameters from outer function
. But when I browse the help text of this error, I can only see cascaded function definitions, but not function calls.
Is a way to get the size of a generic type at compile time? Many thanks for any suggestions! (I am new to Rust, so if I have made some silly mistakes, please point them out directly )
Here is a simplified version of my code:
fn foo<T>(value: T) -> SomeStruct
where T: num_traits::PrimInt + num_traits::Signed {
const BYTES: usize = std::mem::size_of::<T>();
// ...
todo!()
}
Compiler output:
error[E0401]: can't use generic parameters from outer function
--> src\[...].rs:7:46
|
5 | fn foo<T>(value: T) -> SomeStruct
| - type parameter from outer function
6 | where T: num_traits::PrimInt + num_traits::Signed {
7 | const BYTES: usize = std::mem::size_of::<T>();
| ^ use of generic parameter from outer function
For more information about this error, try `rustc --explain E0401`.
error: could not compile `[...]` due to previous error