[][src]Trait print_bytes::ToBytes

pub trait ToBytes<'a> {
#[must_use]    fn to_bytes(&'a self) -> Bytes<'a>;
}

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.

Since Bytes has no public constructor, implementations should call to_bytes on another type to create an instance. If storing owned bytes is necessary, Bytes::from can be used instead to avoid lifetime errors.

Consider also implementing From for Bytes when the type is a smart pointer.

Note: Future Compatibility

This trait should not be implemented for types that also implement Display or ToString. A blanket implementation may eventually be provided to cover those traits automatically, but that would currently be considered a coherence violation.

Examples

use print_bytes::println_bytes;
use print_bytes::Bytes;
use print_bytes::ToBytes;

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

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

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

Required methods

#[must_use]fn to_bytes(&'a self) -> Bytes<'a>

Creates a byte sequence that will be used to represent the instance.

Loading content...

Implementations on Foreign Types

impl<'a> ToBytes<'a> for [u8][src]

impl<'a, const N: usize> ToBytes<'a> for [u8; N][src]

impl<'a, T: ?Sized> ToBytes<'a> for Cow<'a, T> where
    T: ToBytes<'a> + ToOwned,
    T::Owned: ToBytes<'a>, 
[src]

impl<'a> ToBytes<'a> for CStr[src]

impl<'a> ToBytes<'a> for CString[src]

impl<'a> ToBytes<'a> for IoSlice<'a>[src]

impl<'a> ToBytes<'a> for IoSliceMut<'a>[src]

impl<'a> ToBytes<'a> for Vec<u8>[src]

impl<'a> ToBytes<'a> for OsStr[src]

impl<'a> ToBytes<'a> for OsString[src]

impl<'a> ToBytes<'a> for Path[src]

impl<'a> ToBytes<'a> for PathBuf[src]

Loading content...

Implementors

Loading content...