pub struct BufSink<'a> { /* private fields */ }Expand description
A fixed-capacity output sink wrapping a borrowed &mut [u8].
Writes are UTF-8 bytes; on overflow the excess is dropped and
BufSink::overflowed returns true (the buffer is never written past
its end). BufSink::as_str returns the bytes written so far.
Implementations§
Source§impl<'a> BufSink<'a>
impl<'a> BufSink<'a>
Sourcepub fn new(buf: &'a mut [u8]) -> BufSink<'a>
pub fn new(buf: &'a mut [u8]) -> BufSink<'a>
Wrap a byte buffer as an output sink.
Examples found in repository?
examples/disasm.rs (line 55)
31fn emit_word<W: Write>(token: &str, out: &mut W) -> io::Result<()> {
32 let token = token.trim();
33 if token.is_empty() {
34 return Ok(());
35 }
36 // Accept an optional `0x` prefix and surrounding whitespace.
37 let hex = token.trim_start_matches("0x").trim_start_matches("0X");
38 let word = match u32::from_str_radix(hex, 16) {
39 Ok(w) => w,
40 Err(_) => {
41 writeln!(out, "{token}\t<invalid hex>")?;
42 return Ok(());
43 }
44 };
45
46 // Keep the manual diagnostic resilient if an unexpected decoder panic is
47 // introduced: one bad word should not abort a stream of spot checks.
48 let rendered = std::panic::catch_unwind(|| {
49 let bytes = word.to_le_bytes();
50 let mut dec = Decoder::new(&bytes, ADDRESS, DecoderOptions::default());
51 let insn = dec.decode();
52
53 // Zero-alloc format into a fixed stack buffer.
54 let mut buf = [0u8; 256];
55 let mut sink = BufSink::new(&mut buf);
56 FmtFormatter::new().format(&insn, &mut sink);
57 sink.as_str().to_string()
58 })
59 .unwrap_or_else(|_| String::from("<unimplemented>"));
60
61 writeln!(out, "{word:08X}\t{rendered}")?;
62 Ok(())
63}Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
The bytes written so far, as &str (always valid UTF-8 because only
whole &str chunks are appended, truncated only at chunk granularity).
Examples found in repository?
examples/disasm.rs (line 57)
31fn emit_word<W: Write>(token: &str, out: &mut W) -> io::Result<()> {
32 let token = token.trim();
33 if token.is_empty() {
34 return Ok(());
35 }
36 // Accept an optional `0x` prefix and surrounding whitespace.
37 let hex = token.trim_start_matches("0x").trim_start_matches("0X");
38 let word = match u32::from_str_radix(hex, 16) {
39 Ok(w) => w,
40 Err(_) => {
41 writeln!(out, "{token}\t<invalid hex>")?;
42 return Ok(());
43 }
44 };
45
46 // Keep the manual diagnostic resilient if an unexpected decoder panic is
47 // introduced: one bad word should not abort a stream of spot checks.
48 let rendered = std::panic::catch_unwind(|| {
49 let bytes = word.to_le_bytes();
50 let mut dec = Decoder::new(&bytes, ADDRESS, DecoderOptions::default());
51 let insn = dec.decode();
52
53 // Zero-alloc format into a fixed stack buffer.
54 let mut buf = [0u8; 256];
55 let mut sink = BufSink::new(&mut buf);
56 FmtFormatter::new().format(&insn, &mut sink);
57 sink.as_str().to_string()
58 })
59 .unwrap_or_else(|_| String::from("<unimplemented>"));
60
61 writeln!(out, "{word:08X}\t{rendered}")?;
62 Ok(())
63}Sourcepub fn overflowed(&self) -> bool
pub fn overflowed(&self) -> bool
true if any write was truncated due to insufficient capacity.
Trait Implementations§
Auto Trait Implementations§
impl<'a> !UnwindSafe for BufSink<'a>
impl<'a> Freeze for BufSink<'a>
impl<'a> RefUnwindSafe for BufSink<'a>
impl<'a> Send for BufSink<'a>
impl<'a> Sync for BufSink<'a>
impl<'a> Unpin for BufSink<'a>
impl<'a> UnsafeUnpin for BufSink<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more