Skip to content

Commit

Permalink
Rollup merge of #85001 - CDirkx:bytestring, r=JohnTitor
Browse files Browse the repository at this point in the history
Merge `sys_common::bytestring` back into `os_str_bytes`

`bytestring` contains code for correctly debug formatting a byte slice (`[u8]`). This functionality is and has historically only been used to provide the debug formatting of byte-based os-strings (on unix etc.).

Having this functionality in the separate `bytestring` module was useful in the past to reduce duplication, as [when it was added](#46798) `os_str_bytes` was still split into `sys::{unix, redox, wasi, etc.}::os_str`. However, now that is no longer the case, there is not much reason for the `bytestring` functionality to be separate from `os_str_bytes`; I don't think it is very likely that another part of std will need to handle formatting byte strings that are not os-strings in the future (everything should be `utf8`). This is why this PR merges the functionality of `bytestring` directly into the debug implementation in `os_str_bytes`.
  • Loading branch information
JohnTitor committed Jul 2, 2021
2 parents 45470a3 + 059008f commit fb736d9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 50 deletions.
26 changes: 0 additions & 26 deletions library/std/src/sys_common/bytestring.rs

This file was deleted.

19 changes: 0 additions & 19 deletions library/std/src/sys_common/bytestring/tests.rs

This file was deleted.

1 change: 0 additions & 1 deletion library/std/src/sys_common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
mod tests;

pub mod backtrace;
pub mod bytestring;
pub mod condvar;
pub mod fs;
pub mod io;
Expand Down
22 changes: 18 additions & 4 deletions library/std/src/sys_common/os_str_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
//! systems: just a `Vec<u8>`/`[u8]`.

use crate::borrow::Cow;

use crate::fmt;
use crate::fmt::Write;
use crate::mem;
use crate::rc::Rc;
use crate::str;
use crate::sync::Arc;
use crate::sys_common::bytestring::debug_fmt_bytestring;
use crate::sys_common::{AsInner, IntoInner};

use core::str::lossy::Utf8Lossy;
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};

#[cfg(test)]
mod tests;

#[derive(Hash)]
#[repr(transparent)]
Expand All @@ -26,7 +28,19 @@ pub struct Slice {

impl fmt::Debug for Slice {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_fmt_bytestring(&self.inner, formatter)
// Writes out a valid unicode string with the correct escape sequences

formatter.write_str("\"")?;
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(&self.inner).chunks() {
for c in valid.chars().flat_map(|c| c.escape_debug()) {
formatter.write_char(c)?
}

for b in broken {
write!(formatter, "\\x{:02X}", b)?;
}
}
formatter.write_str("\"")
}
}

Expand Down
10 changes: 10 additions & 0 deletions library/std/src/sys_common/os_str_bytes/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use super::*;

#[test]
fn slice_debug_output() {
let input = Slice::from_u8_slice(b"\xF0hello,\tworld");
let expected = r#""\xF0hello,\tworld""#;
let output = format!("{:?}", input);

assert_eq!(output, expected);
}

0 comments on commit fb736d9

Please sign in to comment.