Trait bytes::Source [] [src]

pub trait Source {
    fn copy_to_buf<B: BufMut>(self, buf: &mut B);
}

A value that writes bytes from itself into a BufMut.

Values that implement Source are used as an argument to BufMut::put.

Examples

use bytes::{BufMut, Source};

struct Repeat {
    num: usize,
    str: String,
}

impl Source for Repeat {
    fn copy_to_buf<B: BufMut>(self, buf: &mut B) {
        for _ in 0..self.num {
            buf.put(&self.str);
        }
    }
}

let mut dst = vec![];
dst.put(Repeat {
    num: 3,
    str: "hello".into(),
});

assert_eq!(*dst, b"hellohellohello"[..]);

Required Methods

Copy data from self into destination buffer

Examples

use bytes::{BufMut, Source};

let mut dst = vec![];

"hello".copy_to_buf(&mut dst);

assert_eq!(*dst, b"hello"[..]);

Panics

This function panis if buf does not have enough capacity for self.

Implementors