Repository navigation
Use Write instead of owned strings - #8
Conversation
|
Cool! I see you've picked the Write trait from io instead of fmt. The reason I suggested This would make the first a more natural fit. Adapting one to write to the other is fairly trivial if users have a need. |
|
@CasualX yes! I initially started with fmt but the htmlescaping library uses io so I just did simple find and replace |
|
I didn’t find a way to cast fmt::Write to io::Write, so I just used io. What’s the simple way to do that? |
|
You cannot cast them directly, you need an adapter struct: pub struct IoAdapter<W>(pub W);
impl<W: io::Write> fmt::Write for IoAdapter<W> {
fn write_str(&mut self, s: &str) -> fmt::Result {
io::Write::write_all(&mut self.0, s.as_bytes()).map_err(|_| fmt::Error)
}
fn write_fmt(&mut self, args: fmt::Arguments) -> fmt::Result {
io::Write::write_fmt(&mut self.0, args).map_err(|_| fmt::Error)
}
}
impl IoAdapter<io::Stdout> {
pub fn stdout() -> IoAdapter<io::Stdout> {
IoAdapter(io::stdout())
}
}
impl IoAdapter<io::Stderr> {
pub fn stderr() -> IoAdapter<io::Stderr> {
IoAdapter(io::stderr())
}
}I'm not sure why htmlescape went with io Write, looking at the implementation is fairly trivial how it works. Honestly it's probably faster to render to string first than the character by character thing htmlescape does in the |
…plement own simple html escaping module
This struck a nerve for me. The implementation is trivial, so reimplementing it will probably be a better choice. Now it's back to |
This should give better performance and be more "rusty"
cc @CasualX (as suggested in your comment in Reddit 😄)