base64ct: extract and refactor with Encoding trait#238
Conversation
|
Thought I'd try this approach before adding the final encoding I actually want (the I'd like to allow |
12e44a7 to
4afbe95
Compare
Refactors common encoding functionality into an `Encoding` trait, and defines each variant as a ZST which impls (or rather, receives a blanket impl) of the trait. This makes trait-based abstractions around various encodings possible, and also eliminates a considerable amount of duplication.
4afbe95 to
49cafa8
Compare
newpavlov
left a comment
There was a problem hiding this comment.
I haven't gone over all changes, but on the first glance looks good!
The only suggestion I currently have is to merge Variant and Encoding traits into one and use default method implementations in terms of decode_6bits/encode_6bits. I think it should reduce code amount even further since you no longer need to define the second trait and write a blanket implementation for it.
Also I wonder if we even can go even further. Base64 variants can be generally described with 3-5 u8 ranges mapped into other u8 ranges, so we could in theory define a single constant with this mapping and implement the 6bits methods in terms of it. Though we would have to check if compiler properly inlines use of such constant.
I was trying to avoid exposing this as part of the public API:
...but
Since the purpose of this crate is to avoid LUTs, the only way to do this would be a sort of iterator over various range mapping operations to outputs... (or rather, two such iterators, one for encoding and one for decoding) however depending on circumstances those outputs may or may not include the source value itself (something we could capture in some sort of variant mapping, perhaps). It gets tricky enough I don't really feel an abstraction is helpful, or at least, I haven't found a good one yet. But again, this is all the more reason to wrap this sort of thing up in a sealed trait. With a sealed trait we can experiment with these sorts of changes behind the scenes, whereas without one we'd be exposing it as part of the public API making any such internal refactoring a breaking change. |
|
Ah, I missed the sealed part.
Yes, I thought about having an associated constant slice with arguments for |
Refactors common encoding functionality into an
Encodingtrait, and defines each variant as a ZST which impls (or rather, receives a blanket impl) of the trait.This makes trait-based abstractions around various encodings possible, and also eliminates a considerable amount of duplication.