Skip to content

Commit

Permalink
Replace MappedWrite's magic u8 constant with byte string literal (#780)
Browse files Browse the repository at this point in the history
The `NEWLINE_ASCII_BYTE` constant only really existed because
it's otherwise not obvious that the `0x0Au8` u8 value represents
an ASCII newline byte.

However, we can replace the `0x0Au8` u8 with a byte string literal of
`b'\n'` which is identical in it's value, but much easier to reason about.

The byte string literal form is what the Rust stdlib uses in several places:
https://github.com/rust-lang/rust/blob/1.76.0/library/core/src/num/mod.rs#L1023
https://github.com/rust-lang/rust/blob/1.76.0/library/core/src/escape.rs#L20
https://github.com/rust-lang/rust/blob/1.76.0/library/std/src/io/buffered/linewritershim.rs#L47
  • Loading branch information
edmorley authored Feb 14, 2024
1 parent fbf3919 commit 152b1a2
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions libherokubuildpack/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'static>
MappedWrite::new(w, marker_byte, f)
}

/// Constructs a writer that buffers written data until an ASCII/UTF-8 newline byte (`0x0A`) is
/// Constructs a writer that buffers written data until an ASCII/UTF-8 newline byte (`b'\n'`) is
/// encountered and then applies the given mapping function to the data before passing the result to
/// the wrapped writer.
///
Expand All @@ -25,7 +25,7 @@ pub fn line_mapped<W: io::Write, F: (Fn(Vec<u8>) -> Vec<u8>) + Sync + Send + 'st
w: W,
f: F,
) -> MappedWrite<W> {
mapped(w, NEWLINE_ASCII_BYTE, f)
mapped(w, b'\n', f)
}

/// Constructs a writer that writes to two other writers. Similar to the UNIX `tee` command.
Expand Down Expand Up @@ -155,8 +155,6 @@ impl<A: io::Write, B: io::Write> io::Write for TeeWrite<A, B> {
}
}

const NEWLINE_ASCII_BYTE: u8 = 0x0Au8;

#[cfg(test)]
mod test {
use super::tee;
Expand Down

0 comments on commit 152b1a2

Please sign in to comment.