-
|
As explained in the title, I have a struct with generic // [dependencies]
// crypto-bigint = "0.7.0-rc.25"
// hybrid-array = "0.4.7"
use crypto_bigint::Uint;
use hybrid_array::ArrayN;
pub struct MyStruct<const LIMBS: usize> {}
impl<const LIMBS: usize> MyStruct<LIMBS> {
fn some_function(array: ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }>) {
todo!()
}
fn some_other_function() -> ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }> {
todo!()
}
}
fn main() {}However, this doesn't work: error: generic parameters may not be used in const operations
--> src/main.rs:674:51
|
674 | fn some_function(array: ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }>) {
| ^^^^^ cannot perform const operation using `LIMBS`
|
= help: const parameters may only be used as standalone arguments here, i.e. `LIMBS`
error: generic parameters may not be used in const operations
--> src/main.rs:677:55
|
677 | fn some_other_function() -> ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }> {
| ^^^^^ cannot perform const operation using `LIMBS`
|
= help: const parameters may only be used as standalone arguments here, i.e. `LIMBS`
error: could not compile `rust-tests` (bin "rust-tests") due to 2 previous errors |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
|
I could only make it work on nightly: #![allow(incomplete_features)]
#![feature(generic_const_exprs)]
use crypto_bigint::Uint;
use hybrid_array::{ArrayN, AssocArraySize};
pub struct MyStruct<const LIMBS: usize> {}
#[allow(unused)]
impl<const LIMBS: usize> MyStruct<LIMBS>
where
[u8; Uint::<{ LIMBS }>::BYTES]: AssocArraySize,
{
fn some_function(array: ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }>) {
todo!()
}
fn some_other_function() -> ArrayN<u8, { Uint::<{ LIMBS }>::BYTES }> {
todo!()
}
}
fn main() {} |
Beta Was this translation helpful? Give feedback.
-
[Spoiler] Using macro:use crypto_bigint::Limb;
use hybrid_array::ArrayN;
pub struct MyStruct<const LIMBS: usize> {}
trait MyStructExt {
type MyArrayN;
fn some_function(array: Self::MyArrayN);
fn some_other_function() -> Self::MyArrayN;
}
macro_rules! impl_uint_some_function {
($(($uint:ident, $bytes:path)),+) => {
$(
use crypto_bigint::$uint;
impl MyStructExt for MyStruct<{ $uint::LIMBS }>
{
type MyArrayN = ArrayN<u8, { $uint::LIMBS * Limb::BYTES }>;
fn some_function(array: Self::MyArrayN) {
let _ = array;
todo!()
}
fn some_other_function() -> Self::MyArrayN {
todo!()
}
}
)+
};
}
fn main() {
/// uint 1024 on 64-bit
type MS = MyStruct<16>;
type AN = <MS as MyStructExt>::MyArrayN;
MS::some_function(AN::default());
let uint1024: AN = MS::some_other_function();
let _: ArrayN<u8, 128> = uint1024;
{
use hybrid_array::Array;
use hybrid_array::typenum;
let _: Array<u8, typenum::U128> = uint1024;
}
}
// TODO: use `generic_const_exprs` when stable to make generic around bits.
impl_uint_some_function! {
(U64, typenum::U8),
(U128, typenum::U16),
(U192, typenum::U24),
(U256, typenum::U32),
(U384, typenum::U48),
(U448, typenum::U56),
(U512, typenum::U64),
(U576, typenum::U72),
(U768, typenum::U96),
(U832, typenum::U104),
(U896, typenum::U112),
(U1024, typenum::U128),
(U1536, typenum::U192),
(U1792, typenum::U224),
(U2048, typenum::U256),
(U3072, typenum::U384),
(U3584, typenum::U448),
(U4096, typenum::U512),
(U6144, typenum::U768),
(U8192, typenum::U1024)
} |
Beta Was this translation helpful? Give feedback.
-
|
This is what the |
Beta Was this translation helpful? Give feedback.
This is what the
hybrid-arrayfeature andArrayDecoding/ArrayEncodingtraits are for