I recently decided to start contributing to Rust, and to that end, I
grabbed the code and grepped for FIXME. One of the issues I came across was
#4423: "Make a version of char::escape_unicode that doesn't allocate" (
https://github.com/mozilla/rust/issues/4423).

Of course, we could add a one-off fix to ensure that repr doesn't allocate,
but that would involve duplicating a lot of logic that currently lives in
char, num, and uint-template. Instead, it seems that we need a suitable
replacement for all library functions that return ~str, so that an
allocation-conscious caller can avoid allocating.

One approach would be to instead pass in a fn(char) that is called for each
resulting character in order. Thus, escaping unicode code point 256 would
call fn('\\'), fn('u'), fn('0'), fn('1'), fn('0'), fn('0').

Another approach would be for the functions that return ~str to instead
accept an argument for some general object (like Writer) on which
write_char could be called repeatedly. Then the function would return ()
instead.

I'm partial to the fn approach, as the usage in repr would then look
something like:

do with_escaped_unicode_chars(ch) |c| {
    self.write_char(c);
}

Any thoughts on this?

p.s. I'm very new to Rust development and might not understand the
prevalent paradigms, so anything about this suggestion is off-base, I'd
appreciate a nudge in the right direction. Also, wouldn't it make sense to
have some kind of core-lang docs? Or have I just now looked hard enough?
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to