Skip to main content

DataWriter

Struct DataWriter 

Source
pub struct DataWriter<'a> { /* private fields */ }
Expand description

Zero-copy write cursor over a mutable byte slice.

Position-tracked and bounds-checked. Use this when initializing a new account’s data layout inside a create instruction - it replaces the error-prone pattern of tracking byte offsets by hand.

let mut raw = account.try_borrow_mut()?;
let mut w = DataWriter::new(&mut *raw);
w.write_u8(MY_DISC)?;         // discriminator
w.write_u64(0)?;               // initial balance
w.write_address(&authority)?;  // 32-byte pubkey

All writes are little-endian, matching the convention used by Solana’s instruction data encoding and most on-chain programs.

Implementations§

Source§

impl<'a> DataWriter<'a>

Source

pub fn new(data: &'a mut [u8]) -> Self

Source

pub fn written(&self) -> usize

Number of bytes written so far.

Source

pub fn write_u8(&mut self, val: u8) -> Result<(), ProgramError>

Source

pub fn write_u16(&mut self, val: u16) -> Result<(), ProgramError>

Source

pub fn write_u32(&mut self, val: u32) -> Result<(), ProgramError>

Source

pub fn write_u64(&mut self, val: u64) -> Result<(), ProgramError>

Source

pub fn write_i64(&mut self, val: i64) -> Result<(), ProgramError>

Source

pub fn write_bool(&mut self, val: bool) -> Result<(), ProgramError>

Writes 1u8 for true, 0u8 for false.

Source

pub fn write_address(&mut self, addr: &Address) -> Result<(), ProgramError>

Source

pub fn write_i8(&mut self, val: i8) -> Result<(), ProgramError>

Source

pub fn write_i16(&mut self, val: i16) -> Result<(), ProgramError>

Source

pub fn write_i32(&mut self, val: i32) -> Result<(), ProgramError>

Source

pub fn write_u128(&mut self, val: u128) -> Result<(), ProgramError>

Write a u128 (16 bytes, little-endian).

Source

pub fn write_i128(&mut self, val: i128) -> Result<(), ProgramError>

Write an i128 (16 bytes, little-endian).

Auto Trait Implementations§

§

impl<'a> Freeze for DataWriter<'a>

§

impl<'a> RefUnwindSafe for DataWriter<'a>

§

impl<'a> Send for DataWriter<'a>

§

impl<'a> Sync for DataWriter<'a>

§

impl<'a> Unpin for DataWriter<'a>

§

impl<'a> UnsafeUnpin for DataWriter<'a>

§

impl<'a> !UnwindSafe for DataWriter<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.