Skip to content
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

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

Merged
merged 1 commit into from
Feb 14, 2024

Conversation

edmorley
Copy link
Member

@edmorley edmorley commented Feb 14, 2024

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.)

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
@edmorley edmorley self-assigned this Feb 14, 2024
@edmorley edmorley marked this pull request as ready for review February 14, 2024 13:12
@edmorley edmorley requested a review from a team as a code owner February 14, 2024 13:12
@edmorley edmorley merged commit 152b1a2 into main Feb 14, 2024
6 checks passed
@edmorley edmorley deleted the edmorley/mappedwrite-newline-byte branch February 14, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants