rusticata / asn1-rs

Parsers/Encoders for ASN.1 BER/DER data
Apache License 2.0
10 stars 14 forks source link

Fix receiver lifetimes in `Any` methods. #22

Closed SergioBenitez closed 1 year ago

SergioBenitez commented 1 year ago

The lifetimes in several Any methods are restrictive in a manner that's unlikely intended. To resolve this, this PR makes changes of the following nature:

-    pub fn as_bytes(&'a self) -> &'a [u8] {
+    pub fn as_bytes(&self) -> &'a [u8] {

The prior method signature requires that the reference to receiver have the same lifetime as the internal content of Any. What this means is that Rust will unify the lifetime of the receiver with that of the returned content. Since the receiver must have a shorter lifetime than the content, the lifetime of the returned content is shortened. This is unnecessary as there need not be any restriction on the receiver's lifetime.

SergioBenitez commented 1 year ago

Neither of the failures are related to this change.

chifflier commented 1 year ago

Indeed, it's better to let the compiler infer the lifetimes here. Applied, thanks!

SergioBenitez commented 1 year ago

Indeed, it's better to let the compiler infer the lifetimes here.

Applied, thanks!

This isn't quite related to inference. When you specify the input and output lifetimes with the same name, you ask the compiler to unify them: to find one logical lifetime that works for both references. If instead you name one and not the other, the two lifetimes are now unrelated, and no relationship between the two is required by the compiler.