librasn / compiler

An ASN1 compiler producing Rust bindings for the rasn framework
Other
13 stars 7 forks source link

Fixed size `SEQUENCE OF` types should use `[T; N]` #57

Open XAMPPRocky opened 1 month ago

XAMPPRocky commented 1 month ago

Currently the compiler always uses the SequenceOf type, but it should use different types if its sizes are restricted. Starting with if it has a fixed size it should use an array.

6d7a commented 1 week ago

Thank you for your issue. I was testing this with the following asn1:


IntegerAlias ::= INTEGER (10..14)
FixedSizeAliasSequence ::= SEQUENCE (SIZE (2)) OF IntegerAlias

Which generates the following bindings:

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
#[rasn(delegate, size("4"))]
pub struct FixedSizeIntegerSequence(pub [AnonymousFixedSizeIntegerSequence; 4usize]);

#[derive(AsnType, Debug, Clone, Decode, Encode, PartialEq, Eq, Hash)]
#[rasn(delegate, value("10..=14"))]
pub struct IntegerAlias(pub u8);

However, rasn::Decode requires for [T; 4] that T: Default, so IntegerAlias would need to implement Default. I'm reluctant to derive Default for every type that is used in a fixed size SEQUENCE OF, because of possible conflicts between the derived rust default value and a hypothetical asn1 DEFAULT value for the type in a SEQUENCE or SET.