Trait print_bytes::ToBytes[][src]

pub trait ToBytes {
    fn to_bytes(&self) -> ByteStr<'_>;
fn to_wide(&self) -> Option<WideStr>; }
Expand description

Represents a type similarly to Display.

Implement this trait to allow printing a type that cannot guarantee UTF-8 output. It is used to bound values accepted by functions in this crate.

Examples

use print_bytes::println_bytes;
use print_bytes::ByteStr;
use print_bytes::ToBytes;
#[cfg(windows)]
use print_bytes::WideStr;

struct ByteSlice<'a>(&'a [u8]);

impl ToBytes for ByteSlice<'_> {
    fn to_bytes(&self) -> ByteStr<'_> {
        self.0.to_bytes()
    }

    #[cfg(windows)]
    fn to_wide(&self) -> Option<WideStr> {
        self.0.to_wide()
    }
}

println_bytes(&ByteSlice(b"Hello, world!"));

Required methods

Returns a byte string that will be used to represent the instance.

This is supported on Windows only.

Returns a wide character string that will be used to represent the instance.

The Windows API frequently uses wide character strings. This method allows them to be printed losslessly in some cases, even when they cannot be converted to UTF-8.

Returning None causes to_bytes to be used instead.

Implementations on Foreign Types

This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.
This is supported on Windows only.

Implementors