Struct kex::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)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn print_data_per_byte(data: &[u8], is_little_endian: bool) {
    use std::io::stdout;

    let config = Config::new(
        Some(AddressFormatter::new(AddressStyle::Hex(16), Default::default())),
        ByteFormatter::new(
            Default::default(),
            Groupping::RepeatingGroup(Group::new(GROUP_SIZE, " - "), NUM_OF_GROUPS),
            "/",
            is_little_endian,
            Default::default()
        ),
        Some(CharFormatter::default()),
        false,
    );

    let mut printer = Printer::new(stdout(), 0 as usize, config);

    
    for s in data {
        assert!(printer
            .write(&[*s])
            .is_ok());
    }

    _ = printer.finish();
    println!("");
}
More examples
Hide additional examples
examples/custom.rs (line 25)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    use std::io::stdout;

    let config = Config::new(
        Some(AddressFormatter::new(
            AddressStyle::Dec(8),
            Separators::new("", &'\u{1F929}'.to_string()),
        )),
        ByteFormatter::new(
            Default::default(),
            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
            "",
            false,
            Default::default(),
        ),
        Some(CharFormatter::new(
            ".".to_string(),
            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
        )),
        true
    );
    let mut printer = Printer::new(stdout(), 0, config);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Custom printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
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)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    use std::io::stdout;

    let mut printer = Printer::default_fmt_with(stdout(), 0);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Simple printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
More examples
Hide additional examples
examples/stdin.rs (line 29)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    use std::io::stdout;

    let mut buf = [0u8; 64];
    let stdin = std::io::stdin();
    let mut handle = stdin.lock();
    
    let mut printer = Printer::default_fmt_with(stdout(), 0);

    while let Ok(size) = handle.read(&mut buf) {
        if size == 0 {
            break;
        }
        assert!(printer.write_all(&mut buf[..size]).is_ok());
    }

    printer.finish();
}
examples/endian.rs (line 47)
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
fn print_data_per_byte(data: &[u8], is_little_endian: bool) {
    use std::io::stdout;

    let config = Config::new(
        Some(AddressFormatter::new(AddressStyle::Hex(16), Default::default())),
        ByteFormatter::new(
            Default::default(),
            Groupping::RepeatingGroup(Group::new(GROUP_SIZE, " - "), NUM_OF_GROUPS),
            "/",
            is_little_endian,
            Default::default()
        ),
        Some(CharFormatter::default()),
        false,
    );

    let mut printer = Printer::new(stdout(), 0 as usize, config);

    
    for s in data {
        assert!(printer
            .write(&[*s])
            .is_ok());
    }

    _ = printer.finish();
    println!("");
}
examples/custom.rs (line 41)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    use std::io::stdout;

    let config = Config::new(
        Some(AddressFormatter::new(
            AddressStyle::Dec(8),
            Separators::new("", &'\u{1F929}'.to_string()),
        )),
        ByteFormatter::new(
            Default::default(),
            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
            "",
            false,
            Default::default(),
        ),
        Some(CharFormatter::new(
            ".".to_string(),
            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
        )),
        true
    );
    let mut printer = Printer::new(stdout(), 0, config);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Custom printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
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)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    use std::io::stdout;

    let mut printer = Printer::default_fmt_with(stdout(), 0);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Simple printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
More examples
Hide additional examples
examples/custom.rs (line 32)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
fn main() {
    use std::io::stdout;

    let config = Config::new(
        Some(AddressFormatter::new(
            AddressStyle::Dec(8),
            Separators::new("", &'\u{1F929}'.to_string()),
        )),
        ByteFormatter::new(
            Default::default(),
            Groupping::RepeatingGroup(Group::new(4, "#"), 4),
            "",
            false,
            Default::default(),
        ),
        Some(CharFormatter::new(
            ".".to_string(),
            Separators::new(&'\u{1F4A5}'.to_string(), &'\u{1F4A8}'.to_string()),
        )),
        true
    );
    let mut printer = Printer::new(stdout(), 0, config);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Custom printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
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)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
fn main() {
    use std::io::stdout;

    let mut printer = Printer::default_fmt_with(stdout(), 0);

    let bytes1 = &[222u8, 173, 190, 239];
    let bytes2 = &[0xfeu8, 0xed, 0xfa];
    let title = b"Simple printing";

    for _ in 0..10 {
        _ = printer.push(bytes1);
    }

    _ = printer.push(title);

    for _ in 0..11 {
        _ = printer.push(bytes2);
    }

    printer.finish();
}
More examples
Hide additional examples
examples/stdin.rs (line 20)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fn main() {
    use std::io::stdout;

    let mut buf = [0u8; 64];
    let stdin = std::io::stdin();
    let mut handle = stdin.lock();
    
    let mut printer = Printer::default_fmt_with(stdout(), 0);

    while let Ok(size) = handle.read(&mut buf) {
        if size == 0 {
            break;
        }
        assert!(printer.write_all(&mut buf[..size]).is_ok());
    }

    printer.finish();
}

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>

Write 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, fmt: 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 Selfwhere Self: Sized,

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.