-
Notifications
You must be signed in to change notification settings - Fork 32
Allow custom write errors #567
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Facundo Tuesca <[email protected]>
|
This is going to be related to these being "perfect" derives. If you use |
Signed-off-by: Facundo Tuesca <[email protected]>
Signed-off-by: Facundo Tuesca <[email protected]>
|
Yeah, this is the expanded code for the trait impl that causes the error mentioned above: impl<T: X> asn1::SimpleAsn1Writable for TaggedRequiredFields<T>
where
for<'asn1_internal> asn1::Implicit<
&'asn1_internal T::Type,
1,
>: asn1::Asn1Writable,
for<'asn1_internal> asn1::Explicit<
&'asn1_internal T::Type,
2,
>: asn1::Asn1Writable,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Implicit<&'asn1_internal T::Type, 1> as asn1::Asn1Writable>::Error,
>,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Explicit<&'asn1_internal T::Type, 2> as asn1::Asn1Writable>::Error,
>,
{
type Error = asn1::WriteError;
const TAG: asn1::Tag = <asn1::SequenceWriter as asn1::SimpleAsn1Writable>::TAG;
fn write_data(&self, dest: &mut asn1::WriteBuf) -> Result<(), Self::Error> {
let mut w = asn1::Writer::new(dest);
w.write_element(&asn1::Implicit::<_, 1>::new(&self.a))?;
w.write_element(&asn1::Explicit::<_, 2>::new(&self.b))?;
Ok(())
}
fn data_length(&self) -> Option<usize> {
Some(
0
+ asn1::Asn1Writable::encoded_length(
&asn1::Implicit::<_, 1>::new(&self.a),
)?
+ asn1::Asn1Writable::encoded_length(
&asn1::Explicit::<_, 2>::new(&self.b),
)?,
)
}
}The things that changed are the new trait bounds for the error types, and adding the new error associated type: asn1::WriteError: for<'asn1_internal> From<
<asn1::Implicit<&'asn1_internal T::Type, 1> as asn1::Asn1Writable>::Error,
>,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Explicit<&'asn1_internal T::Type, 2> as asn1::Asn1Writable>::Error,
>,
/......
type Error = asn1::WriteError;
fn write_data(&self, dest: &mut asn1::WriteBuf) -> Result<(), Self::Error> {But as far as I understand, these new bounds should not have any new requirements, since the |
|
Hmm, I don't think you want `asn1::WriteError` on the LHS of a bound, I
think you want to reformulate it with the type on the LHS and RHS being
`Into<WriteError>` (which should work because there's always symmetric
into/from impls)
…On Sat, Jul 5, 2025 at 9:47 AM Facundo Tuesca ***@***.***> wrote:
*facutuesca* left a comment (alex/rust-asn1#567)
<#567 (comment)>
Yeah, this is the expanded code for the trait impl that causes the error
mentioned above:
impl<T: X> asn1::SimpleAsn1Writable for TaggedRequiredFields<T>
where
for<'asn1_internal> asn1::Implicit<
&'asn1_internal T::Type,
1,
>: asn1::Asn1Writable,
for<'asn1_internal> asn1::Explicit<
&'asn1_internal T::Type,
2,
>: asn1::Asn1Writable,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Implicit<&'asn1_internal T::Type, 1> as asn1::Asn1Writable>::Error,
>,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Explicit<&'asn1_internal T::Type, 2> as asn1::Asn1Writable>::Error,
>,
{
type Error = asn1::WriteError;
const TAG: asn1::Tag = <asn1::SequenceWriter as asn1::SimpleAsn1Writable>::TAG;
fn write_data(&self, dest: &mut asn1::WriteBuf) -> Result<(), Self::Error> {
let mut w = asn1::Writer::new(dest);
w.write_element(&asn1::Implicit::<_, 1>::new(&self.a))?;
w.write_element(&asn1::Explicit::<_, 2>::new(&self.b))?;
Ok(())
}
fn data_length(&self) -> Option<usize> {
Some(
0
+ asn1::Asn1Writable::encoded_length(
&asn1::Implicit::<_, 1>::new(&self.a),
)?
+ asn1::Asn1Writable::encoded_length(
&asn1::Explicit::<_, 2>::new(&self.b),
)?,
)
}
}
The things that changed are the new trait bounds for the error types, and
adding the new error associated type:
asn1::WriteError: for<'asn1_internal> From<
<asn1::Implicit<&'asn1_internal T::Type, 1> as asn1::Asn1Writable>::Error,
>,
asn1::WriteError: for<'asn1_internal> From<
<asn1::Explicit<&'asn1_internal T::Type, 2> as asn1::Asn1Writable>::Error,
>,
/......
type Error = asn1::WriteError;
fn write_data(&self, dest: &mut asn1::WriteBuf) -> Result<(), Self::Error> {
But as far as I understand, these new bounds should not have any new
requirements, since the Implicit types mentioned are already constrained
to be Asn1Writable by the existing bounds.
—
Reply to this email directly, view it on GitHub
<#567 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAAGBHR7PC6ZTZJZEURBHT3G7JP7AVCNFSM6AAAAACA27BLTOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTAMZYHE3DCMZXGI>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
All that is necessary for evil to succeed is for good people to do nothing.
|
Signed-off-by: Facundo Tuesca <[email protected]>
I pushed the change, but the error is still the same, plus now the |
|
Hmm, what's the status of this, looks like we haven't quite been able to get it building correctly? Do we have a minimized example of the pattern that doesn't build? |
Attempt at implementing custom error types for encoding, as per the comment in #562 (comment)
This PR:
Asn1Writable,SimpleAsn1WritableandAsn1DefinedByWritabletraits calledError, which specifies the error type for the associatedwritefunctionserror_typeto the derive macroAsn1Write(which will populate the associated type mentioned above)Asn1Writederive macro, adding more trait bounds to the derivedSimpleAsn1Writableimplementation for a typeT.Tto be convertible toT::Error. For example:T::Error: From<FieldOfT::Error>. This is so that an error that happens while writing any of the fields ofTcan be converted to the error type associated withT.However, after adding these new bounds, the compiler now complains about the derived
SimpleAsn1Writableimplementation forT, for the cases where there are fields present maked as IMPLICIT and OPTIONAL.when processing this type in
derive_test:898:I don't quite understand why this is needed now (and not before), and trying to fix it by adding another bound so that
X: SimpleAsn1Writablefor theXinImplicit<X>didn't work.I'm opening this as a draft for reference and discussion.