Skip to content

Add encode_vec(input: T, buf: &mut Vec<u8>) that appends to a Vec instead of a String #302

Description

@Not-A-Normal-Robot
Before you file an issue
  • Did you read the docs? yes, and it only has encode_string,
  • Did you read the README? yes, and it doesn't seem to have a mention about this

The problem

Let's say I want to write using a specific file format that has base64 interspersed with non-utf8 bytes:

......binary stuff......dmFsaWQgYmFzZTY0Cg==......more binary stuff......

Currently this is a bit tricky to do efficiently. None of the current Engine methods have a thing that supports outputting into a &mut Vec<u8>:

  • encode(T) -> String allocates a whole new string, and I'd have to copy it over to my buffer.
  • encode_string(T, &mut String) takes in a &mut String so it may not have to reallocate... but I don't have a String here; I only have a Vec<u8> buffer.
  • encode_slice(T, &mut [u8]) could kinda work? I'd have to extend my Vec<u8> with zeroes, then slice into those zeroes, making sure it has a sufficient size or else it'll return an error.

It would be a lot more convenient if there were an encode_vec(T, &mut Vec<u8>) method. Not only would this skip zeroing out the memory, it also means unlike encode_slice there's not really a reason for it to fail (besides allocator failures but ehhhh)

How I, the issue filer, am going to help solve it

By proposing this function to be added.

Not a Duplicate (probably)

I actually saw issue #257 about adding encode_base64(input: &[u8]) -> Vec<u8>. This allocates a new Vec. Meanwhile I'm actually suggesting a way to append to an existing Vec. So I don't think this is a duplicate. I didn't really find much else about encoding to Vecs when searching for "Vec" in the issue tracker, but maybe I missed something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions