I'm using data types (PubKey
and PrivKey
) from a library and create them in a loop with constant indices 1..NUM_INSTANCES
.
const NUM_INSTANCES: usize = 32;
Those types don't have any default initialization.
I create elements of those types and wanna add those to an array
or ArrayVec
.
1. via Array
let mut private_keys: [PrivKey<Sha256_256>; NUM_INSTANCES];
I can add thee elements inside the loop into the array, e.g.:
priv_keys[idx-1] = priv_key;
But if I try to use those elements later in a loop the following way as an argument to a function:
&priv_keys[idx-1].as_slice()
then I get the following (of course correct) message:
used binding priv_keys isn't initialized
I don't think I can initialize an array properly. I could create fake keys and try to add them in a loop to initialize the array...
2. via ArrayVec
Another approach was to use an ArrayVec
:
let mut priv_keys: ArrayVec<[PrivKey<Sha256_256>; NUM_INSTANCES]>;
and push()
the created elements after creating:
priv_keys.push(priv_key);
but that yielded the following message:
the method `push` exists for struct `ArrayVec<[PrivKey<Sha256_256>; 32]>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`[my_lib::PrivKey<my_lib::Sha256_256>; 32]: Array`
I don't really understand this message...I can't find anything regarding a trait Array
:
My questions
- I'd like to understand the message with the "trait bounds"
- Any ideas what's a good way to achieve what I wanna do, assuming that I can't change the types
PrivKey
andPubKey
?