Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Added an example of direct BorshSerialize::serialize usage with vector and slice buffers #29

Merged
merged 1 commit into from Apr 12, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions borsh/src/ser/mod.rs
Expand Up @@ -16,6 +16,27 @@ use std::{rc::Rc, sync::Arc};
const DEFAULT_SERIALIZER_CAPACITY: usize = 1024;

/// A data-structure that can be serialized into binary format by NBOR.
///
/// ```
/// use borsh::BorshSerialize;
///
/// #[derive(BorshSerialize)]
/// struct MyBorshSerializableStruct {
/// value: String,
/// }
///
/// let x = MyBorshSerializableStruct { value: "hello".to_owned() };
/// let mut buffer: Vec<u8> = Vec::new();
/// x.serialize(&mut buffer).unwrap();
/// let single_serialized_buffer_len = buffer.len();
///
/// x.serialize(&mut buffer).unwrap();
/// assert_eq!(buffer.len(), single_serialized_buffer_len * 2);
///
/// let mut buffer: Vec<u8> = vec![0; 1024 + single_serialized_buffer_len];
/// let mut buffer_slice_enough_for_the_data = &mut buffer[1024..1024 + single_serialized_buffer_len];
/// x.serialize(&mut buffer_slice_enough_for_the_data).unwrap();
/// ```
pub trait BorshSerialize {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>;

Expand Down