-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array::from_slice deprecated #114
Comments
Please show a code snippet demonstrating the issue. |
I'm sorry I'm half wrong about what I said but there is still a some points in my issue let's take this code as an example: use ml_kem::array::Array;
use ml_kem::{
kem::{Encapsulate, EncapsulationKey},
Encoded, EncodedSizeUser, KemCore as _, MlKem1024, MlKem1024Params,
};
// using ml-kem = "0.2.1"
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut rng = rand::thread_rng();
let (dk, ek): (_, EncapsulationKey<MlKem1024Params>) = MlKem1024::generate(&mut rng);
let ek_as_bytes: Array<u8, _> = ek.as_bytes(); // rust-analyzer unable to deduct Array's second generic argument it's show as Array<u8, {unknow}>
let ek_as_u8_vec: Vec<u8> = ek_as_bytes.to_vec();
// send ek_as_u8_vec over internet to client
{
// client side code
let encoded_ek = Encoded::<EncapsulationKey<MlKem1024Params>>::try_from(&ek_as_u8_vec)?; // Error
let encoded_ek = Encoded::<EncapsulationKey<MlKem1024Params>>::try_from(ek_as_u8_vec)?; // Error
let encoded_ek =
Encoded::<EncapsulationKey<MlKem1024Params>>::try_from(ek_as_u8_vec.as_slice())?; // Good
let ek = EncapsulationKey::<MlKem1024Params>::from_bytes(&ek_as_u8_vec.try_into()?); // Error
let ek = EncapsulationKey::<MlKem1024Params>::from_bytes(&encoded_ek); // Good
let ek =
EncapsulationKey::<MlKem1024Params>::from_bytes(ek_as_u8_vec.as_slice().try_into()?); // Good
let ek =
EncapsulationKey::<MlKem1024Params>::from_bytes(&ek_as_u8_vec.as_slice().try_into()?); // Good
let (cipher_text, shared_key) = ek.encapsulate(&mut rng).unwrap();
let cipher_text_vec = cipher_text.to_vec();
// send cipher_text_back_to server
cipher_text_vec
};
// the rest of logic
Ok(())
} I expect this code to compile there are 2 main problems
I'm on rust 1.84.0 stable |
Unfortunately, it's not possible to provide a generic It's unfortunate that the compiler fails to apply deref in this case, while it works for built-in arrays: use hybrid_array::{Array, sizes::U4};
let vec: Vec<u8> = vec![1, 2, 3, 4];
// works
let key1: [u8; 4] = vec.try_into().unwrap();
// fails
let key2: Array<u8, U4> = vec.try_into().unwrap(); I am not exactly sure what exactly causes this difference (probably the
It's an expected behavior. The same would happen with built-in arrays. |
I would still argue that most people think Plus I would always just write Since compiler isn't able to recognize Or maybe just adding a |
We can definitely add a section to the documentation about alternatives to In a future release we could also potentially consider adding back a |
Array::from_slice is deprecated it's suggested to use Array::try_from but try::from &[u8] is not satisfied.
I encountered frustration using
ml-kem
crate trying to serialize and deserialize encapsulation key and the cipher_textI was unable to find any standard way to achieve that so I just turned off the warning by adding
#[allow(deprecated)]
The text was updated successfully, but these errors were encountered: