Skip to content
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

Add convenience TextEncoder functions to encode directly to string #402

Merged
merged 2 commits into from Jul 27, 2021

Conversation

matklad
Copy link
Contributor

@matklad matklad commented Jul 8, 2021

I saw that we do

        let mut buffer = vec![];
        let encoder = TextEncoder::new();
        encoder.encode(&prometheus::gather(), &mut buffer).unwrap();

        String::from_utf8(buffer).unwrap()

a bunch of times in near and figured that the library can probably guaranty utf8 validity and remove the need for one unwrap here.

This turned out to be slightly more involved then I've thought, as there's no standard way to explain that you'll only write utf8 to a Write, but it wasn't to hard either.

As a bonus, TextEncoder impl now is statically guaranteed to produce only valid utf8

@matklad matklad force-pushed the text-encoder branch 2 times, most recently from b73f78e to 062224b Compare July 9, 2021 10:13
src/encoder/text.rs Outdated Show resolved Hide resolved
src/encoder/text.rs Outdated Show resolved Hide resolved
@lucab
Copy link
Member

lucab commented Jul 9, 2021

Thanks for PR. Looks great overall, I've left a couple of minor comments in review.
It looks like our CI is having a bad day; I'll try to have a look at fixing it as soon as I have a free timeslot, then this PR may need to be rebased to see a green run.

@matklad
Copy link
Contributor Author

matklad commented Jul 9, 2021

Sure, no problem rebasing!

FYI, I see the "1 workflow awaiting approval" banner, if you want to disable for new contributors, you can check this box:

image

@matklad
Copy link
Contributor Author

matklad commented Jul 9, 2021

Figured out a much better solution -- there's no need for unsafety here.

@lucab
Copy link
Member

lucab commented Jul 19, 2021

Ok, #403 is merged so rebasing this PR now should make the CI happy.

@lucab
Copy link
Member

lucab commented Jul 19, 2021

@zhouqiang-cl @BusyJay @breeswish whoever of you has access to do that, can you please tweak the workflow permissions as shown in #402 (comment) so that we don't have to re-approve it on every push?

@BusyJay
Copy link
Member

BusyJay commented Jul 21, 2021

As github actions resource are bound to the organization account, so it might not be a good idea to just change it for a repository. Maybe you can send an issue to tikv/community or a thread at https://internals.tidb.io/, let's discuss it more!

Signed-off-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Signed-off-by: Aleksey Kladov <aleksey.kladov@gmail.com>
@matklad
Copy link
Contributor Author

matklad commented Jul 26, 2021

@lucab ptal :)

Copy link
Member

@lucab lucab left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lucab lucab merged commit 9bbd908 into tikv:master Jul 27, 2021
@matklad matklad deleted the text-encoder branch July 27, 2021 11:08
@lucab lucab mentioned this pull request Sep 27, 2021
JanBerktold pushed a commit to JanBerktold/rust-prometheus that referenced this pull request Nov 12, 2022
…ikv#402)

* Convenience TextEncoder functions for working with strings

Signed-off-by: Aleksey Kladov <aleksey.kladov@gmail.com>

* Avoid accidentally quadratic issue in TextEncoder::encode_utf8

Signed-off-by: Aleksey Kladov <aleksey.kladov@gmail.com>
Signed-off-by: Jan Berktold <jberktold@roblox.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants