Struct Clippet

Source
pub struct Clippet { /* private fields */ }
Expand description

A structure for containing chunks of data and information about it. The label field is useful for storing things like MIME types. The size field is automatically increased when you append to the data field.

Implementations§

Source§

impl Clippet

Source

pub fn get_label(&self) -> String

Returns the label field.

let data = Clippet { label : "Hello!".to_string(), ... };
assert_eq!(data.get_label(), "Hello!")
Source

pub fn set_label(&mut self, label: &str) -> bool

Changes the label field.

let data = Clippet { ... };
assert_eq!(data.set_label("Hello!"), true);
Source

pub fn add_bytes(&mut self, bytes: BytesMut)

Appends a sequence of bytes to the Clippet structure. Also increases the size field by one.

let bytes = BytesMut::from("Hello, world!");
let mut data = Clippet { ... };
data.add_bytes(bytes);
Source

pub fn drop_bytes(&mut self, index: usize)

Removes a sequence of bytes from the Clippet structure. Also decreases the size field by one.

let mut data = Clippet { ... };
data.get_size() // e.g. 10
data.drop_bytes(5) // drops 6th sequence of bytes
data.get_size() // 9
Source

pub fn get_size(&self) -> i32

Returns the size of the data field.

let data = Clippet { ... };
assert_eq!(data.get_size(), 0);
Source

pub fn get_data(&self, index: usize) -> BytesMut

Returns a sequence of bytes.

let bytes = BytesMut::from("Hello, world!");
let mut data = Clippet { ... };
data.add_bytes(bytes);
assert_eq!(data.get_data(0), bytes);
Source

pub fn get_as_string(&self, index: usize) -> String

Takes an indexed byte sequence and returns it as a String.

let bytes = BytesMut::from("Hello, world!");
let mut data = Clippet { ... };
data.add_bytes(bytes);
assert_eq!(data.get_as_string(0), "Hello, world!");
Source

pub fn from_file(name: &str, size: usize) -> Clippet

Takes a filename and splits it into sequences of bytes within a Clippet. It is recommended to use smaller byte sequences, however this is user-configurable.

let data = Clippet::from_file("example.txt", 512);
Source

pub fn from_bytes(bytes: BytesMut) -> Clippet

Takes a sequence of bytes and wraps it around a Clippet structure.

let data = Clippet::from_bytes(foo);
Source

pub fn len(&self) -> i32

Alias of get_size(&self)

Source

pub fn from_string(str: &str, size: usize) -> Clippet

Takes a string and splits it into sequences of bytes within a Clippet. It is recommended to use smaller byte sequences, however this is user-configurable. This uses a string reference.

let data = Clippet::from_string("Lorem ipsum dolor amet...", 512);
Source

pub fn combine(self) -> String

Consumes the Clippet, combines all of its byte sequences and outputs them as a String literal.

let data = Clippet::from_string("Hello, World!", 512);
println!("{}", data.combine()); /// Hello, World!
Source

pub fn dechunk(self) -> Clippet

Replaces the Clippet with one that has all of its data in one chunk. The label is retained, the data is not modified apart from being dechunked.

let data = Clippet::from_string("HelloWorld", 5); // [b"Hello", b"World"]
let data = data.dechunk(); // [b"HelloWorld"]
Source

pub fn rechunk(self, size: usize) -> Clippet

Combines the Clippet and then remakes it, retaining the label, with the specified max chunk size. No data is modified.

let longstring = "long string bla bla bla..."; // Pretend this is 2KB large!
let data = Clippet::from_string(longstring, 512); // size: 4
let data = data.rechunk(1024) // size: 2

Trait Implementations§

Source§

impl Debug for Clippet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Source§

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.