Struct no_proto::NP_Factory [−][src]
Factories are created from schemas. Once you have a factory you can use it to create new buffers or open existing ones.
The easiest way to create a factory is to pass a JSON string schema into the static new
method. Learn about schemas here.
You can also create a factory with a compiled byte schema using the static new_bytes
method.
Example
use no_proto::error::NP_Error; use no_proto::NP_Factory; let user_factory = NP_Factory::new(r#" struct({fields: { name: string(), pass: string(), age: u16(), todos: list({of: 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.new_buffer(None); // optional capacity, optional address size // set the "name" field of the struct user_buffer.set(&["name"], "Billy Joel")?; // set the first todo user_buffer.set(&["todos", "0"], "Write a rust library.")?; // close buffer let user_vec:Vec<u8> = user_buffer.finish().bytes(); // open existing buffer for reading let user_buffer_2 = user_factory.open_buffer(user_vec); // read field name let name_field = user_buffer_2.get::<&str>(&["name"])?; assert_eq!(name_field, Some("Billy Joel")); // read first todo let todo_value = user_buffer_2.get::<&str>(&["todos", "0"])?; assert_eq!(todo_value, Some("Write a rust library.")); // read second todo let todo_value = user_buffer_2.get::<&str>(&["todos", "1"])?; assert_eq!(todo_value, None); // close buffer again let user_vec: Vec<u8> = user_buffer_2.finish().bytes(); // user_vec is a serialized Vec<u8> with our data
Next Step
Read about how to use buffers to access, mutate and compact data.
Fields
schema: NP_Schema
schema data used by this factory
Implementations
impl NP_Factory
[src]
pub fn new<S>(es6_schema: S) -> Result<Self, NP_Error> where
S: Into<String>,
[src]
S: Into<String>,
Generate a new factory from an ES6 schema
The operation will fail if the string can’t be parsed or the schema is otherwise invalid.
pub fn new_json<S>(json_schema: S) -> Result<Self, NP_Error> where
S: Into<String>,
[src]
S: Into<String>,
Generate a new factory from the given JSON 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 new_bytes(schema_bytes: &[u8]) -> Result<Self, NP_Error>
[src]
Create a new factory from a compiled schema byte array. The byte schemas are at least an order of magnitude faster to parse than JSON schemas.
pub fn export_schema_bytes(&self) -> &[u8]
[src]
Get a copy of the compiled schema byte array
pub fn export_schema_idl(&self) -> Result<String, NP_Error>
[src]
Exports this factorie’s schema to ES6 IDL. This works regardless of wether the factory was created with NP_Factory::new
or NP_Factory::new_bytes
.
pub fn export_schema_json(&self) -> Result<NP_JSON, NP_Error>
[src]
Exports this factorie’s schema to JSON. This works regardless of wether the factory was created with NP_Factory::new
or NP_Factory::new_bytes
.
pub fn open_buffer(&self, bytes: Vec<u8>) -> NP_Buffer
[src]
Open existing Vec
pub fn open_buffer_ref<'buffer>(
&'buffer self,
bytes: &'buffer [u8]
) -> NP_Buffer
[src]
&'buffer self,
bytes: &'buffer [u8]
) -> NP_Buffer
Open existing buffer as ready only ref, can much faster if you don’t need to mutate anything.
All operations that would lead to mutation fail. You can’t perform any mutations on a buffer opened with this method.
Also, read only buffers are Sync
and Send
so good for multithreaded environments.
pub fn open_buffer_ref_mut<'buffer>(
&'buffer self,
bytes: &'buffer mut [u8],
data_len: usize
) -> NP_Buffer
[src]
&'buffer self,
bytes: &'buffer mut [u8],
data_len: usize
) -> NP_Buffer
Open existing buffer as mutable ref, can be much faster to skip copying. The data_len
property is how many bytes the data in the buffer is using up.
Some mutations cannot be done without appending bytes to the existing buffer. Since it’s impossible to append bytes to a &mut [u8]
type, you should provide mutable slice with extra bytes on the end if you plan to mutate the buffer.
The data_len
is at which byte the data ends in the buffer, this will be moved as needed by compaction and mutation operations.
If the &mut [u8]
type has the same length as data_len
, mutations that require additional bytes will fail. &mut [u8].len() - data_len
is how many bytes the buffer has for new allocations.
pub fn new_buffer<'buffer>(&'buffer self, capacity: Option<usize>) -> NP_Buffer
[src]
Generate a new empty buffer from this factory.
The first opional argument, capacity, can be used to set the space of the underlying Vec
pub fn new_buffer_ref_mut<'buffer>(
&'buffer self,
bytes: &'buffer mut [u8]
) -> NP_Buffer
[src]
&'buffer self,
bytes: &'buffer mut [u8]
) -> NP_Buffer
Generate a new empty buffer from this factory.
Make sure the mutable slice is large enough to fit all the data you plan on putting into it.
pub fn pack_buffer(&self, buffer: NP_Buffer) -> NP_Packed_Buffer
[src]
Convert a regular buffer into a packed buffer. A “packed” buffer contains the schema and the buffer data together.
You can optionally store buffers with their schema attached so you don’t have to track the schema seperatly.
The schema is stored in a very compact, binary format. A JSON version of the schema can be generated from the binary version at any time.
Trait Implementations
Auto Trait Implementations
impl Unpin for NP_Factory
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,
pub 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.
pub 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>,