Struct no_proto::buffer::NP_Buffer[][src]

pub struct NP_Buffer {
    pub mutable: bool,
    // some fields omitted
}

Buffers contain the bytes of each object and allow you to perform reads, updates, deletes and compaction.

Fields

mutable: bool

Is this buffer mutable?

Implementations

impl NP_Buffer[src]

pub fn json_encode(&self, path: &[&str]) -> Result<NP_JSON, NP_Error>[src]

Copy an object at the provided path and all it’s children into JSON.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new(r#"
    struct({fields: {
        age: u8(),
        name: string()
    }})
"#)?;
 
let mut new_buffer = factory.new_buffer(None);
new_buffer.set(&["name"], "Jeb Kermin");
new_buffer.set(&["age"], 30u8);
 
assert_eq!(r#"{"value":{"age":30,"name":"Jeb Kermin"}}"#, new_buffer.json_encode(&[])?.stringify());
assert_eq!(r#"{"value":"Jeb Kermin"}"#, new_buffer.json_encode(&["name"])?.stringify());
 

pub fn finish(self) -> NP_Finished_Buffer[src]

Finish the buffer.

If the buffer is an onwed type typically opened with .open_buffer or created with .new_empty you will get the bytes of the buffer returned from this method.

If the buffer is a ref type typically opened with .open_buffer_ref or .open_buffer_ref_mut this method returns an empty Vec<u8>.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("string()")?;
 
let mut new_buffer = factory.new_buffer(None);
// set initial value
new_buffer.set(&[], "hello")?;
// close buffer and get bytes
let bytes: Vec<u8> = new_buffer.finish().bytes();
assert_eq!([0, 0, 0, 0, 0, 6, 0, 0, 0, 5, 104, 101, 108, 108, 111].to_vec(), bytes);
 

pub fn read_bytes(&self) -> &[u8][src]

Read the bytes of the buffer immutably. No touching!

pub fn move_cursor(&mut self, path: &[&str]) -> Result<bool, NP_Error>[src]

Move buffer cursor to new location. Cursors can only be moved into children. If you need to move up reset the cursor to root, then move back down to the desired level.

This also creates objects/collections along the path as needed. If you attempt to move into a path that doesn’t exist, this method will return false. Otherwise it will return true of the path requested exists or is something that can be made to exist.

pub fn cursor_to_root(&mut self)[src]

Moves cursor position to root of buffer, the default.

pub fn set_max(&mut self, path: &[&str]) -> Result<bool, NP_Error>[src]

Set the max value allowed for the specific data type at the given key.

String & Byte types only work if a size property is set in the schema.

Will return true if a value was found and succesfully set, false otherwise.

WARNING If you call this on a collection (Map, Tuple, List, or Table) ALL children will be overwritten/set. The method is recursive, so this will hit all children, including nested children.

When this is applied to a string data type, only ascii values are supported.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
 
let factory: NP_Factory = NP_Factory::new(r#"
    tuple({
        sorted: true,
        values: [string({size: 10}), u32()]
    })
"#)?;
 
let mut low_buffer = factory.new_buffer(None);
// set all types to minimum value
low_buffer.set_min(&[])?;
// get bytes
let low_bytes: Vec<u8> = low_buffer.finish().bytes();
 
let mut high_buffer = factory.new_buffer(None);
// set all types to max value
high_buffer.set_max(&[])?;
// get bytes
let high_bytes: Vec<u8> = high_buffer.finish().bytes();
 
let mut middle_buffer = factory.new_buffer(None);
middle_buffer.set(&["0"], "Light This Candle!");
middle_buffer.set(&["1"], 22938u32);
let middle_bytes: Vec<u8> = middle_buffer.finish().bytes();
 
assert!(low_bytes < middle_bytes);
assert!(middle_bytes < high_bytes);
assert!(low_bytes < high_bytes);
 
 

pub fn set_min(&mut self, path: &[&str]) -> Result<bool, NP_Error>[src]

Set the min value allowed for the specific data type at the given key.

String & Byte types only work if a size property is set in the schema.

Will return true if a value was found and succesfully set, false otherwise.

WARNING If you call this on a collection (Map, Tuple, List, or Table) ALL children will be overwritten/set. The method is recursive, so this will hit all children, including nested children.

When this is applied to a string data type, only ascii values are supported.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
 
let factory: NP_Factory = NP_Factory::new(r#"
    tuple({
        sorted: true,
        values: [string({size: 10}), u32()]
    })
"#)?;
 
let mut low_buffer = factory.new_buffer(None);
// set all types to minimum value
low_buffer.set_min(&[])?;
// get bytes
let low_bytes: Vec<u8> = low_buffer.finish().bytes();
 
let mut high_buffer = factory.new_buffer(None);
// set all types to max value
high_buffer.set_max(&[])?;
// get bytes
let high_bytes: Vec<u8> = high_buffer.finish().bytes();
 
let mut middle_buffer = factory.new_buffer(None);
middle_buffer.set(&["0"], "Light This Candle!");
middle_buffer.set(&["1"], 22938u32);
let middle_bytes: Vec<u8> = middle_buffer.finish().bytes();
 
assert!(low_bytes < middle_bytes);
assert!(middle_bytes < high_bytes);
assert!(low_bytes < high_bytes);
 
 

pub fn set<'set, X: 'set>(
    &mut self,
    path: &[&str],
    value: X
) -> Result<bool, NP_Error> where
    X: NP_Value<'set> + NP_Scalar<'set>, 
[src]

Used to set scalar values inside the buffer.

The type that you set with will be compared to the schema, if it doesn’t match the schema the request will fail.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
// a list where each item is a map where each key has a value containing a list of strings
let factory: NP_Factory = NP_Factory::new(r#"list({of: map({ value: list({ of: string() })})})"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// third item in the top level list -> key "alpha" of map at 3rd element -> 9th element of list at "alpha" key
// 
new_buffer.set(&["3", "alpha", "9"], "look at all this nesting madness")?;
 
// get the same item we just set
let message = new_buffer.get::<&str>(&["3", "alpha", "9"])?;
 
assert_eq!(message, Some("look at all this nesting madness"));
 

pub fn set_with_json<S: Into<String>>(
    &mut self,
    path: &[&str],
    json_value: S
) -> Result<bool, NP_Error>
[src]

Set value with JSON

This works with all types including portals.

Data that doesn’t align with the schema will be ignored. Null and undefined values will be ignored.

Partial updates just merge the provided values into the buffer, you only need to provide the values you’d like changed. This method cannot be used to delete values.

Using the .set() method is far more performant. I recommend only using this on the client side of your application.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("list({of: string()})")?;
 
let mut new_buffer = factory.new_buffer(None);
new_buffer.set_with_json(&[], r#"{"value": ["foo", "bar", null, "baz"]}"#)?;
    
assert_eq!(new_buffer.get_length(&[])?, Some(4));
assert_eq!(new_buffer.get::<&str>(&["0"])?, Some("foo"));
assert_eq!(new_buffer.get::<&str>(&["1"])?, Some("bar"));
assert_eq!(new_buffer.get::<&str>(&["2"])?, None);
assert_eq!(new_buffer.get::<&str>(&["3"])?, Some("baz"));
 
new_buffer.set_with_json(&["2"], r#"{"value": "bazzy"}"#)?;
assert_eq!(new_buffer.get::<&str>(&["2"])?, Some("bazzy"));
 
 

pub fn get_collection<'iter>(
    &'iter self,
    path: &'iter [&str]
) -> Result<Option<NP_Generic_Iterator<'iter>>, NP_Error>
[src]

Get an iterator for a collection

List Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("list({of: string()})")?;
 
let mut new_buffer = factory.new_buffer(None);
// set value at 1 index
new_buffer.set(&["1"], "hello")?;
// set value at 4 index
new_buffer.set(&["4"], "world")?;
// push value onto the end
new_buffer.list_push(&[], "!")?;
 
// get iterator of root (list item)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
    match item.index {
        0 => assert_eq!(item.get::<&str>().unwrap(), None),
        1 => assert_eq!(item.get::<&str>().unwrap(), Some("hello")),
        2 => assert_eq!(item.get::<&str>().unwrap(), None),
        3 => assert_eq!(item.get::<&str>().unwrap(), None),
        4 => assert_eq!(item.get::<&str>().unwrap(), Some("world")),
        5 => assert_eq!(item.get::<&str>().unwrap(), Some("!")),
        _ => panic!()
    };
});
 

Struct Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new_json(r#"{
   "type": "struct",
   "fields": [
        ["age", {"type": "uint8"}],
        ["name", {"type": "string"}],
        ["job", {"type": "string"}],
        ["tags", {"type": "list", "of": {"type": "string"}}]
    ]
}"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// set value of age
new_buffer.set(&["age"], 20u8)?;
// set value of name
new_buffer.set(&["name"], "Bill Kerman")?;
// push value onto tags list
new_buffer.list_push(&["tags"], "rocket")?;
 
// get iterator of root (table)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
     
    match item.key {
        "name" => assert_eq!(item.get::<&str>().unwrap(), Some("Bill Kerman")),
        "age" =>  assert_eq!(item.get::<u8>().unwrap(), Some(20)),
        "job" => assert_eq!(item.get::<&str>().unwrap(), None),
        "tags" => { /* tags field is list, can't do anything with it here */ },
        _ => { panic!() }
    };
});
 
// we can also loop through items of the tags list
new_buffer.get_collection(&["tags"])?.unwrap().into_iter().for_each(|item| {
    assert_eq!(item.index, 0);
    assert_eq!(item.get::<&str>().unwrap(), Some("rocket"));
});
 

Map Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new_json(r#"{
   "type": "map",
   "value": {"type": "string"}
}"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// set value of color key
new_buffer.set(&["color"], "blue")?;
// set value of sport key
new_buffer.set(&["sport"], "soccor")?;
 
// get iterator of root (map)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
     
    match item.key {
        "color" => assert_eq!(item.get::<&str>().unwrap(), Some("blue")),
        "sport" => assert_eq!(item.get::<&str>().unwrap(), Some("soccor")),
        _ => panic!()
    }
});
 

Tuple Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new_json(r#"{
   "type": "tuple",
    "values": [
        {"type": "string"},
        {"type": "u8"},
        {"type": "bool"}
    ]
}"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// set value at 0 index
new_buffer.set(&["0"], "hello")?;
// set value at 2 index
new_buffer.set(&["2"], false)?;
 
// get iterator of root (tuple item)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
    match item.index {
        0 => assert_eq!(item.get::<&str>().unwrap(), Some("hello")),
        1 => assert_eq!(item.get::<u8>().unwrap(), None),
        2 => assert_eq!(item.get::<bool>().unwrap(), Some(false)),
        _ => panic!()
    };
});
 

pub fn list_push<'push, X: 'push>(
    &mut self,
    path: &[&str],
    value: X
) -> Result<Option<u16>, NP_Error> where
    X: NP_Value<'push> + NP_Scalar<'push>, 
[src]

Push a value onto the end of a list. The path provided must resolve to a list type, and the type being pushed must match the schema

This is the most efficient way to add values to a list type.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new_json(r#"{
   "type": "list",
    "of": {"type": "string"}
}"#)?;
 
let mut new_buffer = factory.new_buffer(None);
new_buffer.set(&["3"], "launch")?;
new_buffer.list_push(&[], "this")?;
new_buffer.list_push(&[], "rocket")?;
 
// get iterator of root (list item)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
    match item.index {
        0 => assert_eq!(item.get::<&str>().unwrap(), None),
        1 => assert_eq!(item.get::<&str>().unwrap(), None),
        2 => assert_eq!(item.get::<&str>().unwrap(), None),
        3 => assert_eq!(item.get::<&str>().unwrap(), Some("launch")),
        4 => assert_eq!(item.get::<&str>().unwrap(), Some("this")),
        5 => assert_eq!(item.get::<&str>().unwrap(), Some("rocket")),
        _ => panic!()
    };
});
 
let mut new_buffer = factory.new_buffer(None);
new_buffer.list_push(&[], "launch")?;
new_buffer.list_push(&[], "this")?;
new_buffer.list_push(&[], "rocket")?;
 
// get iterator of root (list item)
new_buffer.get_collection(&[])?.unwrap().into_iter().for_each(|item| {
    match item.index {
        0 => assert_eq!(item.get::<&str>().unwrap(), Some("launch")),
        1 => assert_eq!(item.get::<&str>().unwrap(), Some("this")),
        2 => assert_eq!(item.get::<&str>().unwrap(), Some("rocket")),
        _ => panic!()
    };
});
 

pub fn get_length(&self, path: &[&str]) -> Result<Option<usize>, NP_Error>[src]

Get length of String, Bytes, Table, Tuple, List or Map Type

If the type found at the path provided does not support length operations, you’ll get None.

If there is no value at the path provodid, you will get None.

If an item is found and it’s length is zero, you can expect Some(0).

String Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("string()")?;
 
let mut new_buffer = factory.new_buffer(None);
// set initial value
new_buffer.set(&[], "hello")?;
// get length of value at root (String)
assert_eq!(new_buffer.get_length(&[])?, Some(5));
 

Collection (List) Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("list({ of: string() })")?;
 
let mut new_buffer = factory.new_buffer(None);
// set value at 9th index
new_buffer.set(&["9"], "hello")?;
// get length of value at root (List)
assert_eq!(new_buffer.get_length(&[])?, Some(10));
 

Collection (Table) Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new(r#"
    struct({fields: {
        age: u8(),
        name: string()
    }})
"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// get length of value at root (Table)
assert_eq!(new_buffer.get_length(&[])?, Some(2));
 

Collection (Map) Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("map({value: string() })")?;
 
let mut new_buffer = factory.new_buffer(None);
// set values
new_buffer.set(&["foo"], "bar")?;
new_buffer.set(&["foo2"], "bar2")?;
// get length of value at root (Map)
assert_eq!(new_buffer.get_length(&[])?, Some(2));
 

Collection (Tuple) Example

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("tuple({values: [string(), string()]})")?;
 
let mut new_buffer = factory.new_buffer(None);
// get length of value at root (Tuple)
assert_eq!(new_buffer.get_length(&[])?, Some(2));
 

pub fn del(&mut self, path: &[&str]) -> Result<bool, NP_Error>[src]

Clear an inner value from the buffer. This can also be used to clear deeply nested collection objects or scalar objects.

Returns true if it found a value to delete (and deleted it), false otherwise.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("list({ of: string() })")?;
 
let mut new_buffer = factory.new_buffer(None);
// set index 0
new_buffer.set(&["0"], "hello")?;
// del index 0
new_buffer.del(&["0"])?;
// value is gone now!
assert_eq!(None, new_buffer.get::<&str>(&["0"])?);
 

pub fn get_schema_type(
    &self,
    path: &[&str]
) -> Result<Option<NP_TypeKeys>, NP_Error>
[src]

Retrieve the schema type at a given path.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::schema::NP_TypeKeys;
 
let factory: NP_Factory = NP_Factory::new("tuple({values: [ geo8(), dec({exp: 2}), string() ]})")?;
 
let mut new_buffer = factory.new_buffer(None);
 
assert_eq!(new_buffer.get_schema_type(&[])?.unwrap(), NP_TypeKeys::Tuple);
assert_eq!(new_buffer.get_schema_type(&["0"])?.unwrap(), NP_TypeKeys::Geo);
assert_eq!(new_buffer.get_schema_type(&["1"])?.unwrap(), NP_TypeKeys::Decimal);
assert_eq!(new_buffer.get_schema_type(&["2"])?.unwrap(), NP_TypeKeys::UTF8String);
 

pub fn get_schema_default<'get, X: 'get>(
    &'get self,
    path: &[&str]
) -> Result<Option<X>, NP_Error> where
    X: NP_Value<'get> + NP_Scalar<'get>, 
[src]

Retrieve the schema default at a given path.

This is useful for geo and dec data types where there is information about the value in the schema.

For example, when you create an NP_Geo type to put into a geo field, you must know the resolution (4/8/16). If you use this method you can get an empty NP_Geo type that already has the correct resolution set based on the schema.

The type that you cast the request to will be compared to the schema, if it doesn’t match the schema the request will fail.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::pointer::dec::NP_Dec;
use no_proto::pointer::geo::NP_Geo;
 
// a list where each item is a map where each key has a value containing a list of strings
let factory: NP_Factory = NP_Factory::new(r#"
    tuple({values: [
        geo8(),
        dec({exp: 2})
    ]})
"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// Get an empty NP_Geo type that has the correct resolution for the schema
// 
let geo_default: NP_Geo = new_buffer.get_schema_default::<NP_Geo>(&["0"])?.unwrap();
assert_eq!(geo_default.size, 8); // geo is size 8 in schema
 
// Get an empty NP_Dec type that has the correct exp for the schema
// 
let dec_default: NP_Dec = new_buffer.get_schema_default::<NP_Dec>(&["1"])?.unwrap();
assert_eq!(dec_default.exp, 2); // exponent is 2 in schema
 

pub fn get<'get, X: 'get>(
    &'get self,
    path: &[&str]
) -> Result<Option<X>, NP_Error> where
    X: NP_Value<'get> + NP_Scalar<'get>, 
[src]

Retrieve an inner value from the buffer.

The type that you cast the request to will be compared to the schema, if it doesn’t match the schema the request will fail.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
// a list where each item is a map where each key has a value containing a list of strings
let factory: NP_Factory = NP_Factory::new(r#"list({of: map({ value: list({of: string() }) })})"#)?;
 
let mut new_buffer = factory.new_buffer(None);
// third item in the top level list -> key "alpha" of map at 3rd element -> 9th element of list at "alpha" key
// 
new_buffer.set(&["3", "alpha", "9"], "who would build a schema like this")?;
 
// get the same item we just set
let message = new_buffer.get::<&str>(&["3", "alpha", "9"])?;
 
assert_eq!(message, Some("who would build a schema like this"));
 

pub fn maybe_compact<F>(
    &mut self,
    new_capacity: Option<usize>,
    callback: F
) -> Result<(), NP_Error> where
    F: FnMut(NP_Size_Data) -> bool, 
[src]

This performs a compaction if the closure provided as the second argument returns true. Compaction is a pretty expensive operation (requires full copy of the whole buffer) so should be done sparingly. The closure is provided an argument that contains the original size of the buffer, how many bytes could be saved by compaction, and how large the new buffer would be after compaction. The closure should return true to perform compaction, false otherwise.

The first argument, new_capacity, is the capacity of the underlying Vec that we’ll be copying the data into. The default is the size of the old buffer.

WARNING Your cursor location will be reset to the root.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("string()")?;
 
let mut new_buffer = factory.new_buffer(None);
// set initial value
new_buffer.set(&[], "hello")?;
// using 15 bytes
assert_eq!(NP_Size_Data {
    current_buffer: 15,
    after_compaction: 15,
    wasted_bytes: 0
}, new_buffer.calc_bytes()?);
// update the value
new_buffer.set(&[], "hello, world")?;
// now using 31 bytes, with 9 bytes of wasted space
assert_eq!(NP_Size_Data {
    current_buffer: 31,
    after_compaction: 22,
    wasted_bytes: 9
}, new_buffer.calc_bytes()?);
// compact to save space
new_buffer.maybe_compact(None, |compact_data| {
    // only compact if wasted bytes are greater than 5
    if compact_data.wasted_bytes > 5 {
        true
    } else {
        false
    }
})?;
// back down to 18 bytes with no wasted bytes
assert_eq!(NP_Size_Data {
    current_buffer: 22,
    after_compaction: 22,
    wasted_bytes: 0
}, new_buffer.calc_bytes()?);
 

pub fn compact<'compact>(
    &mut self,
    new_capacity: Option<usize>
) -> Result<(), NP_Error>
[src]

Compacts a buffer to remove an unused bytes or free space after a mutation. This is a pretty expensive operation (requires full copy of the whole buffer) so should be done sparingly.

The first argument, new_capacity, is the capacity of the underlying Vec that we’ll be copying the data into. The default is the size of the old buffer.

  • If this buffer is an owned type typically created with new_buffer or opened with open_buffer the comapction will occur into the existing buffer.
  • If this buffer is a ref type typically opened with open_buffer_ref the compaction will fail. Use compact_into instead.
  • If this buffer is a mutable ref type typically opened with open_buffer_ref_mut the compaction will ocurr into the existing buffer and the length will be updated.

WARNING Your cursor location will be reset to the root.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("string()")?;
 
let mut new_buffer = factory.new_buffer(None);
// set initial value
new_buffer.set(&[], "hello")?;
// using 15 bytes
assert_eq!(NP_Size_Data {
    current_buffer: 15,
    after_compaction: 15,
    wasted_bytes: 0
}, new_buffer.calc_bytes()?);
// update the value
new_buffer.set(&[], "hello, world")?;
// now using 31 bytes, with 9 bytes of wasted space
assert_eq!(NP_Size_Data {
    current_buffer: 31,
    after_compaction: 22,
    wasted_bytes: 9
}, new_buffer.calc_bytes()?);
// compact to save space
new_buffer.compact(None)?;
// back down to 18 bytes with no wasted bytes
assert_eq!(NP_Size_Data {
    current_buffer: 22,
    after_compaction: 22,
    wasted_bytes: 0
}, new_buffer.calc_bytes()?);
 

pub fn compact_into(
    &mut self,
    new_capacity: Option<usize>
) -> Result<NP_Buffer, NP_Error>
[src]

Compact the current buffer into a new owned buffer. Returns an owned buffer of the compacted result.

This works identically to .compact except compaction happens into a new buffer, instead of into the existing buffer.

If the buffer was opened as read only with .open_buffer_ref this is the only way to do compaction.

pub fn copy_buffer(&self) -> NP_Buffer[src]

Copy the current buffer into a new owned buffer.

pub fn calc_bytes<'bytes>(&self) -> Result<NP_Size_Data, NP_Error>[src]

Recursively measures how many bytes each element in the buffer is using. This will let you know how many bytes can be saved from a compaction.

use no_proto::error::NP_Error;
use no_proto::NP_Factory;
use no_proto::NP_Size_Data;
 
let factory: NP_Factory = NP_Factory::new("string()")?;
 
let mut new_buffer = factory.new_buffer(None);
new_buffer.set(&[], "hello")?;
assert_eq!(NP_Size_Data {
    current_buffer: 15,
    after_compaction: 15,
    wasted_bytes: 0
}, new_buffer.calc_bytes()?);
 

pub fn set_max_data_length(&mut self, len: usize)[src]

Set the maximum allowed of size of this buffer, in bytes.

Once this value is set, the buffer will not be allowed to grow beyond this size.

This doesn’t cause any mutations, if the buffer is already larger than this value nothing will happen.

pub fn data_length(&self) -> usize[src]

Get the number of bytes used by the data in this buffer.

This will be identical to buffer.read_bytes().len() unless you’re using a RefMut buffer.

Trait Implementations

impl Clone for NP_Buffer[src]

impl Debug for NP_Buffer[src]

impl Send for NP_Buffer[src]

Auto Trait Implementations

impl !Sync for NP_Buffer

impl Unpin for NP_Buffer

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.