heroku / libcnb.rs

A framework for writing Cloud Native Buildpacks in Rust
BSD 3-Clause "New" or "Revised" License
34 stars 6 forks source link

Replace `MappedWrite`'s magic u8 constant with byte string literal #780

Closed edmorley closed 6 months ago

edmorley commented 6 months ago

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#L46

(I spotted this whilst reviewing #721's usage of MappedWrite - since I think BuildpackOutput::start_stream might also need to start handling newline bytes in the closure passed to line_mapped, and was otherwise thinking we'd need to expose the NEWLINE_ASCII_BYTE constant to build_output, until I realised we didn't really need the magic 0x0Au8 value after all.)