Trait print_bytes::ToBytes

source ·
pub trait ToBytes {
    // Required methods
    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_lossy;
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_lossy(&ByteSlice(b"Hello, world!"));

Required Methods§

source

fn to_bytes(&self) -> ByteStr<'_>

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

source

fn to_wide(&self) -> Option<WideStr>

Available 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§

source§

impl ToBytes for CString

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for Vec<u8>

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for CStr

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for OsStr

Available on crate feature os_str_bytes only.
source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for OsString

Available on crate feature os_str_bytes only.
source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for Path

Available on crate feature os_str_bytes only.
source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for PathBuf

Available on crate feature os_str_bytes only.
source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl ToBytes for [u8]

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl<T> ToBytes for Cow<'_, T>
where T: ?Sized + ToBytes + ToOwned, T::Owned: ToBytes,

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.
source§

impl<const N: usize> ToBytes for [u8; N]

source§

fn to_bytes(&self) -> ByteStr<'_>

source§

fn to_wide(&self) -> Option<WideStr>

Available on Windows only.

Implementors§