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
impl Clippet
Sourcepub fn get_label(&self) -> String
pub fn get_label(&self) -> String
Returns the label field.
let data = Clippet { label : "Hello!".to_string(), ... };
assert_eq!(data.get_label(), "Hello!")
Sourcepub fn set_label(&mut self, label: &str) -> bool
pub fn set_label(&mut self, label: &str) -> bool
Changes the label field.
let data = Clippet { ... };
assert_eq!(data.set_label("Hello!"), true);
Sourcepub fn add_bytes(&mut self, bytes: BytesMut)
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);
Sourcepub fn drop_bytes(&mut self, index: usize)
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
Sourcepub fn get_size(&self) -> i32
pub fn get_size(&self) -> i32
Returns the size of the data field.
let data = Clippet { ... };
assert_eq!(data.get_size(), 0);
Sourcepub fn get_data(&self, index: usize) -> BytesMut
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);
Sourcepub fn get_as_string(&self, index: usize) -> String
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!");
Sourcepub fn from_file(name: &str, size: usize) -> Clippet
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);
Sourcepub fn from_bytes(bytes: BytesMut) -> Clippet
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);
Sourcepub fn from_string(str: &str, size: usize) -> Clippet
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);
Sourcepub fn combine(self) -> String
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!
Sourcepub fn dechunk(self) -> Clippet
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"]
Sourcepub fn rechunk(self, size: usize) -> Clippet
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