[−][src]Struct no_proto::NP_Factory
Factories are created from schemas. Once you have a factory you can use it to create new buffers or open existing ones.
The correct way to create a factory is to pass a JSON string schema into the static new
method. Learn about schemas here.
Example
use no_proto::error::NP_Error; use no_proto::NP_Factory; let user_factory = NP_Factory::new(r#"{ "type": "table", "columns": [ ["name", {"type": "string"}], ["pass", {"type": "string"}], ["age", {"type": "uint16"}], ["todos", {"type": "list", "of": {"type": "string"}}] ] }"#)?; // user_factory can now be used to make or open buffers that contain the data in the schema. // create new buffer let mut user_buffer = user_factory.empty_buffer(None, None); // optional capacity, optional size // set the "name" column of the table user_buffer.deep_set("name", "Billy Joel".to_owned())?; // set the first todo user_buffer.deep_set("todos.0", "Write a rust library.".to_owned())?; // close buffer let user_vec:Vec<u8> = user_buffer.close(); // open existing buffer for reading let user_buffer_2 = user_factory.open_buffer(user_vec); // read column value let name_column = user_buffer_2.deep_get::<String>("name")?; assert_eq!(name_column, Some(Box::new("Billy Joel".to_owned()))); // read first todo let todo_value = user_buffer_2.deep_get::<String>("todos.0")?; assert_eq!(todo_value, Some(Box::new("Write a rust library.".to_owned()))); // read second todo let todo_value = user_buffer_2.deep_get::<String>("todos.1")?; assert_eq!(todo_value, None); // close buffer again let user_vec: Vec<u8> = user_buffer_2.close(); // user_vec is a Vec<u8> with our data
Next Step
Read about how to use buffers to access, mutate and compact data.
Implementations
impl NP_Factory
[src]
pub fn new(json_schema: &str) -> Result<NP_Factory, NP_Error>
[src]
Generate a new factory from the given schema.
This operation will fail if the schema provided is invalid or if the schema is not valid JSON. If it fails you should get a useful error message letting you know what the problem is.
pub fn open_buffer(&self, bytes: Vec<u8>) -> NP_Buffer
[src]
Open existing Vec
This just moves the Vec
pub fn empty_buffer(
&self,
capacity: Option<usize>,
ptr_size: Option<NP_Size>
) -> NP_Buffer
[src]
&self,
capacity: Option<usize>,
ptr_size: Option<NP_Size>
) -> NP_Buffer
Generate a new empty buffer from this factory.
The first opional argument, capacity, can be used to set the space of the underlying Vec
The second optional argument, ptr_size, controls how much address space you get in the buffer. NP_Size::U16
(the default) gives you an address space of just over 16KB but is more space efficeint since the address pointers are only 2 bytes each. NP_Size::U32
gives you an address space of just over 4GB, but the addresses take up twice as much space in the buffer.
You can change the address size through compaction after the buffer is created, so it's fine to start with a smaller address space and convert it to a larger one later as needed. It's also possible to go the other way, you can convert larger address space down to a smaller one durring compaction.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,