Struct Printer

Source
pub struct Printer<O: Write, A: AddressFormatting + Clone, B: ByteFormatting + Clone, C: CharFormatting + Clone> { /* private fields */ }
Expand description

The topmost struct for data output

Implementations§

Source§

impl<O: Write, A: AddressFormatting + Clone, B: ByteFormatting + Clone, C: CharFormatting + Clone> Printer<O, A, B, C>

Source

pub fn new( out: O, start_address: usize, config: Config<A, B, C>, ) -> Printer<O, A, B, C>

Customized constructor.

All constructors of the Printer moves given output. To give it back use finish(mut self) function

out - place to ouput string.

start_address - start address to print.

config - formatting configuration.

Printer does no assumptions on start_address where to start reading data, it just recieving data chunks in push(...) function, then increments the start_address

Examples found in repository?
examples/endian.rs (line 38)
22fn print_data_per_byte(data: &[u8], is_little_endian: bool) {
23    use std::io::stdout;
24
25    let config = Config::new(
26        Some(AddressFormatter::new(AddressStyle::Hex(16), Default::default())),
27        ByteFormatter::new(
28            Default::default(),
29            Groupping::RepeatingGroup(Group::new(GROUP_SIZE, " - "), NUM_OF_GROUPS),
30            "/",
31            is_little_endian,
32            Default::default()
33        ),
34        Some(CharFormatter::default()),
35        false,
36    );
37
38    let mut printer = Printer::new(stdout(), 0 as usize, config);
39
40    
41    for s in data {
42        assert!(printer
43            .write(&[*s])
44            .is_ok());
45    }
46
47    _ = printer.finish();
48    println!("");
49}
More examples
Hide additional examples
examples/custom.rs (line 25)
4fn main() {
5    use std::io::stdout;
6
7    let config = Config::new(
8        Some(AddressFormatter::new(
9            AddressStyle::Dec(8),
10            Separators::new("", &'\u{1F929}'.to_string()),
11        )),
12        ByteFormatter::new(
13            Default::default(),
14            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
15            "",
16            false,
17            Default::default(),
18        ),
19        Some(CharFormatter::new(
20            ".".to_string(),
21            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
22        )),
23        true
24    );
25    let mut printer = Printer::new(stdout(), 0, config);
26
27    let bytes1 = &[222u8, 173, 190, 239];
28    let bytes2 = &[0xfeu8, 0xed, 0xfa];
29    let title = b"Custom printing";
30
31    for _ in 0..10 {
32        _ = printer.push(bytes1);
33    }
34
35    _ = printer.push(title);
36
37    for _ in 0..11 {
38        _ = printer.push(bytes2);
39    }
40
41    printer.finish();
42}
Source

pub fn finish(self) -> O

Finalize manually. Prints last unfinished line with paddings and turns back given output

Examples found in repository?
examples/simple.rs (line 23)
4fn main() {
5    use std::io::stdout;
6
7    let mut printer = Printer::default_fmt_with(stdout(), 0);
8
9    let bytes1 = &[222u8, 173, 190, 239];
10    let bytes2 = &[0xfeu8, 0xed, 0xfa];
11    let title = b"Simple printing";
12
13    for _ in 0..10 {
14        _ = printer.push(bytes1);
15    }
16
17    _ = printer.push(title);
18
19    for _ in 0..11 {
20        _ = printer.push(bytes2);
21    }
22
23    printer.finish();
24}
More examples
Hide additional examples
examples/stdin.rs (line 29)
13fn main() {
14    use std::io::stdout;
15
16    let mut buf = [0u8; 64];
17    let stdin = std::io::stdin();
18    let mut handle = stdin.lock();
19    
20    let mut printer = Printer::default_fmt_with(stdout(), 0);
21
22    while let Ok(size) = handle.read(&mut buf) {
23        if size == 0 {
24            break;
25        }
26        assert!(printer.write_all(&mut buf[..size]).is_ok());
27    }
28
29    printer.finish();
30}
examples/endian.rs (line 47)
22fn print_data_per_byte(data: &[u8], is_little_endian: bool) {
23    use std::io::stdout;
24
25    let config = Config::new(
26        Some(AddressFormatter::new(AddressStyle::Hex(16), Default::default())),
27        ByteFormatter::new(
28            Default::default(),
29            Groupping::RepeatingGroup(Group::new(GROUP_SIZE, " - "), NUM_OF_GROUPS),
30            "/",
31            is_little_endian,
32            Default::default()
33        ),
34        Some(CharFormatter::default()),
35        false,
36    );
37
38    let mut printer = Printer::new(stdout(), 0 as usize, config);
39
40    
41    for s in data {
42        assert!(printer
43            .write(&[*s])
44            .is_ok());
45    }
46
47    _ = printer.finish();
48    println!("");
49}
examples/custom.rs (line 41)
4fn main() {
5    use std::io::stdout;
6
7    let config = Config::new(
8        Some(AddressFormatter::new(
9            AddressStyle::Dec(8),
10            Separators::new("", &'\u{1F929}'.to_string()),
11        )),
12        ByteFormatter::new(
13            Default::default(),
14            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
15            "",
16            false,
17            Default::default(),
18        ),
19        Some(CharFormatter::new(
20            ".".to_string(),
21            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
22        )),
23        true
24    );
25    let mut printer = Printer::new(stdout(), 0, config);
26
27    let bytes1 = &[222u8, 173, 190, 239];
28    let bytes2 = &[0xfeu8, 0xed, 0xfa];
29    let title = b"Custom printing";
30
31    for _ in 0..10 {
32        _ = printer.push(bytes1);
33    }
34
35    _ = printer.push(title);
36
37    for _ in 0..11 {
38        _ = printer.push(bytes2);
39    }
40
41    printer.finish();
42}
Source§

impl<O: Write, A: AddressFormatting + Clone, B: ByteFormatting + Clone, C: CharFormatting + Clone> Printer<O, A, B, C>

Source

pub fn push(&mut self, bytes: &[u8]) -> Result<usize>

Accepts bytes chunk. Immediately prints first and second columns to out, third will printed after second column is completely filled, or after finalization.

Examples found in repository?
examples/simple.rs (line 14)
4fn main() {
5    use std::io::stdout;
6
7    let mut printer = Printer::default_fmt_with(stdout(), 0);
8
9    let bytes1 = &[222u8, 173, 190, 239];
10    let bytes2 = &[0xfeu8, 0xed, 0xfa];
11    let title = b"Simple printing";
12
13    for _ in 0..10 {
14        _ = printer.push(bytes1);
15    }
16
17    _ = printer.push(title);
18
19    for _ in 0..11 {
20        _ = printer.push(bytes2);
21    }
22
23    printer.finish();
24}
More examples
Hide additional examples
examples/custom.rs (line 32)
4fn main() {
5    use std::io::stdout;
6
7    let config = Config::new(
8        Some(AddressFormatter::new(
9            AddressStyle::Dec(8),
10            Separators::new("", &'\u{1F929}'.to_string()),
11        )),
12        ByteFormatter::new(
13            Default::default(),
14            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
15            "",
16            false,
17            Default::default(),
18        ),
19        Some(CharFormatter::new(
20            ".".to_string(),
21            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
22        )),
23        true
24    );
25    let mut printer = Printer::new(stdout(), 0, config);
26
27    let bytes1 = &[222u8, 173, 190, 239];
28    let bytes2 = &[0xfeu8, 0xed, 0xfa];
29    let title = b"Custom printing";
30
31    for _ in 0..10 {
32        _ = printer.push(bytes1);
33    }
34
35    _ = printer.push(title);
36
37    for _ in 0..11 {
38        _ = printer.push(bytes2);
39    }
40
41    printer.finish();
42}
Source§

impl<O: Write, A: AddressFormatting + Clone + Default, B: ByteFormatting + Clone + Default, C: CharFormatting + Clone + Default> Printer<O, A, B, C>

Source

pub fn default_with(out: O, start_address: usize) -> Printer<O, A, B, C>

Source§

impl<O: Write> Printer<O, AddressFormatter, ByteFormatter, CharFormatter>

Source

pub fn default_fmt_with( out: O, start_address: usize, ) -> Printer<O, AddressFormatter, ByteFormatter, CharFormatter>

Examples found in repository?
examples/simple.rs (line 7)
4fn main() {
5    use std::io::stdout;
6
7    let mut printer = Printer::default_fmt_with(stdout(), 0);
8
9    let bytes1 = &[222u8, 173, 190, 239];
10    let bytes2 = &[0xfeu8, 0xed, 0xfa];
11    let title = b"Simple printing";
12
13    for _ in 0..10 {
14        _ = printer.push(bytes1);
15    }
16
17    _ = printer.push(title);
18
19    for _ in 0..11 {
20        _ = printer.push(bytes2);
21    }
22
23    printer.finish();
24}
More examples
Hide additional examples
examples/stdin.rs (line 20)
13fn main() {
14    use std::io::stdout;
15
16    let mut buf = [0u8; 64];
17    let stdin = std::io::stdin();
18    let mut handle = stdin.lock();
19    
20    let mut printer = Printer::default_fmt_with(stdout(), 0);
21
22    while let Ok(size) = handle.read(&mut buf) {
23        if size == 0 {
24            break;
25        }
26        assert!(printer.write_all(&mut buf[..size]).is_ok());
27    }
28
29    printer.finish();
30}

Trait Implementations§

Source§

impl<O: Write, A: AddressFormatting + Clone, B: ByteFormatting + Clone, C: CharFormatting + Clone> Drop for Printer<O, A, B, C>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<O: Write, A: AddressFormatting + Clone, B: ByteFormatting + Clone, C: CharFormatting + Clone> Write for Printer<O, A, B, C>

Source§

fn flush(&mut self) -> Result<()>

Does nothing. Always returns Ok(())

Source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<O, A, B, C> Freeze for Printer<O, A, B, C>
where O: Freeze, B: Freeze, A: Freeze, C: Freeze,

§

impl<O, A, B, C> RefUnwindSafe for Printer<O, A, B, C>

§

impl<O, A, B, C> Send for Printer<O, A, B, C>
where O: Send, B: Send, A: Send, C: Send,

§

impl<O, A, B, C> Sync for Printer<O, A, B, C>
where O: Sync, B: Sync, A: Sync, C: Sync,

§

impl<O, A, B, C> Unpin for Printer<O, A, B, C>
where O: Unpin, B: Unpin, A: Unpin, C: Unpin,

§

impl<O, A, B, C> UnwindSafe for Printer<O, A, B, C>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.