Open nickray opened 3 years ago
I'm coming at this from the perspective of having need for an allocation-free X509 certificate generator, or more generally, the "DER encoder to end the plethora of semi-done DER encoders" :)
I'd really like to have this as well. Have you checked out the der
crate? I've been doing quite a bit of work on it lately...
yup, comparing der and x509 with my own stuff is where this originates
I think der::Encodable solves this kind-of at compile time, by requiring the encoded_length method in the trait, which someone has to implement on composite types (at least I think that's what happens - do correct me if I'm wrong).
Composite types don't actually have to implement Encodable
. Rather, there's a higher-level Message
trait which composite types can use so long as their fields are Encodable
, and a blanket impl of Encodable
for Message
which constructs a sequence.
The API is callback-based to allow construction of intermediate field encoder types if necessary. Otherwise composite types only need to construct a slice of trait objects which represent their fields.
I'm coming at this from the perspective of having need for an allocation-free X509 certificate generator, or more generally, the "DER encoder to end the plethora of semi-done DER encoders" :)
The feedback is that
collectable
(or a related trait for random insert e.ginsertable
orspliceable
) should should have some kind of "(overlapping) memmove" trait (and not just anVec::insert
-style operation that is called repeatedly).std::Vec
hassplice
, which is (maybe still?) slow: https://internals.rust-lang.org/t/add-vec-insert-slice-at-to-insert-the-content-of-a-slice-at-an-arbitrary-index/11008. Inheapless-bytes
, I implemented aninsert_slice_at
by hand: https://docs.rs/heapless-bytes/0.1.1/src/heapless_bytes/lib.rs.html#141-156.A need arises in allocation-free TLV encoding (to know L, need to encode V or somehow know its encoding's length first - both in the same pre-allocated buffer). I think
der::Encodable
solves this kind-of at compile time, by requiring theencoded_length
method in the trait, which someone has to implement on composite types (at least I think that's what happens - do correct me if I'm wrong). So you could easily do ato_heapless_vec
implementation there; I assume part of the motivation forcollectable
is to abstract over this and get ato_collectable
method.Whereas
derp::Der::nested
(which I think is an extraction fromring
) andx509::der::der_tlv
both do an allocation. I fix this for my current use cases inasn1derpy::Der::nested
(a temporary fork ofderp
) exactly by doing aninsert_slice_at
.But I'd really like a shared combinator-style approach (like
x509
) without the allocations. Ideally thender
would grow to cover both fixed types and dynamic encoding. Given both these libraries have prestigiously short names :)The goal for me is an API where the generic
n: usize
capacity parameter (orN: ArrayLength<u8>
en attendantmin_const_generics
) only needs to be specified once in the return type. Saying this from brute-forcing a heapless implementation ofx509.rs
in https://github.com/nickray/x509.rs/blob/heapless/src/der.rs#L107-L116, which has multiple issues:collectable
could avoid)N
everywherex509
'sder_tlv
implementation (which could be replaced byasn1derpy
's mem-move, usingcollectable
, if this had some kind ofinsert_iterator_at
/ efficientsplice
.What do you think? :)