-
Notifications
You must be signed in to change notification settings - Fork 136
Avoid std::fmt in serialization code #190
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
Conversation
It hasn’t been used anywhere for a long time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r=me, with comments addressed.
Thanks!
src/serializer.rs
Outdated
@@ -90,7 +75,7 @@ impl<'a> ToCss for Token<'a> { | |||
serialize_unquoted_url(&**value, dest)?; | |||
dest.write_str(")")?; | |||
}, | |||
Token::Delim(value) => write!(dest, "{}", value)?, | |||
Token::Delim(value) => dest.write_str(value.encode_utf8(&mut [0_u8; 4]))?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this use write_char
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why I didn’t think of that… Done :)
src/serializer.rs
Outdated
impl<W: fmt::Write> io::Write for AssumeUtf8<W> { | ||
#[inline] | ||
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | ||
// Safety: itoa only emits ASCII |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we assert it?
debug_assert!(buf.iter().all(|b| b.is_ascii()));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. (AsciiExt
is also implemented on [u8]
, no need to iterate ourselves.)
b3 = [b'\\', to_hex_byte(ascii_byte), b' ']; | ||
&b3[..] | ||
}; | ||
dest.write_str(unsafe { str::from_utf8_unchecked(&bytes) }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could use write_char
, I think, but not sure if worth it, this would be faster and it's trivially ascii so...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When writing more than one byte/char it might be better to minimize the number of calls, but I haven’t measured.
\\1 \\2 \\3 \\4 \\5 \\6 \\7 \\8 \\9 \\A \\B \\C \\D \\E \\F \\10 \ | ||
\\11 \\12 \\13 \\14 \\15 \\16 \\17 \\18 \\19 \\1A \\1B \\1C \\1D \\1E \\1F \\20 \ | ||
\\1 \\2 \\3 \\4 \\5 \\6 \\7 \\8 \\9 \\a \\b \\c \\d \\e \\f \\10 \ | ||
\\11 \\12 \\13 \\14 \\15 \\16 \\17 \\18 \\19 \\1a \\1b \\1c \\1d \\1e \\1f \\20 \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe run this through try to ensure this doesn't break anything? (seems unlikely)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ll do a Gecko Try run before landing in Servo.
… since it involves costly dynamic dispatch.
@bors-servo r=emilio |
📌 Commit c42f384 has been approved by |
Avoid std::fmt in serialization code <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/rust-cssparser/190) <!-- Reviewable:end -->
☀️ Test successful - status-travis |
This change is