Struct manual_serializer::Serializer
source · pub struct Serializer { /* private fields */ }
Expand description
Serializer struct containing a serialization buffer. Please note that this struct expects to have a sufficiently large buffer to perform the ongoing serialization.
Implementations§
source§impl Serializer
impl Serializer
sourcepub fn new(len: usize) -> Serializer
pub fn new(len: usize) -> Serializer
Create a new Serializer
struct with len
byte buffer
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the current byte length of the ongoing serialization (cursor position)
Examples found in repository?
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
pub fn offset(&mut self, offset: usize) -> &mut Self {
if self.cursor + offset >= self.len() {
}
self.cursor += offset;
self
}
/// Try advance the cursor by `offset` bytes. Since the underlying
/// buffer is zero-initialized, skipped bytes will remain as zero.
pub fn try_offset(&mut self, offset: usize) -> Result<&mut Self> {
if self.cursor + offset >= self.data.len() {
return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
}
self.cursor += offset;
Ok(self)
}
/// Advance the cursor by `offset` bytes while explicitly setting
/// skipped bytes to zero. This can be useful if manually positioning
/// cursor within the buffer and the repositioning can result in
/// the buffer containing previously serialized data.
pub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self {
for _ in 0..offset {
self.store_u8(0);
}
self
}
/// Try advance the cursor by `offset` bytes while explicitly setting
/// skipped bytes to zero. This can be useful if manually positioning
/// cursor within the buffer and the repositioning can result in
/// the buffer containing previously serialized data.
pub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self> {
if self.cursor + offset >= self.data.len() {
return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
}
for _ in 0..offset {
self.store_u8(0);
}
Ok(self)
}
sourcepub fn as_slice<'slice>(&'slice self) -> &'slice [u8] ⓘ
pub fn as_slice<'slice>(&'slice self) -> &'slice [u8] ⓘ
Returns a slice &[u8]
of the currently serialized data
sourcepub fn offset(&mut self, offset: usize) -> &mut Self
pub fn offset(&mut self, offset: usize) -> &mut Self
Advance the cursor by offset
bytes. Since the underlying
buffer is zero-initialized, skipped bytes will remain as zero.
Examples found in repository?
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
pub fn align_u32(&mut self) -> &mut Self {
let offset = self.cursor % 4;
self.offset(offset)
}
/// Try advance the cursor to ensure that the current cursor position
/// is on the 32-bit alignment boundary.
pub fn try_align_u32(&mut self) -> Result<&mut Self> {
let offset = self.cursor % 4;
self.try_offset(offset)
}
/// Advance the cursor to ensure that the current cursor position
/// is on the 64-bit alignment boundary.
pub fn align_u64(&mut self) -> &mut Self {
let offset = self.cursor % 8;
self.offset(offset)
}
sourcepub fn try_offset(&mut self, offset: usize) -> Result<&mut Self>
pub fn try_offset(&mut self, offset: usize) -> Result<&mut Self>
Try advance the cursor by offset
bytes. Since the underlying
buffer is zero-initialized, skipped bytes will remain as zero.
Examples found in repository?
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
pub fn try_align_u32(&mut self) -> Result<&mut Self> {
let offset = self.cursor % 4;
self.try_offset(offset)
}
/// Advance the cursor to ensure that the current cursor position
/// is on the 64-bit alignment boundary.
pub fn align_u64(&mut self) -> &mut Self {
let offset = self.cursor % 8;
self.offset(offset)
}
/// Try advance the cursor to ensure that the current cursor position
/// is on the 64-bit alignment boundary.
pub fn try_align_u64(&mut self) -> Result<&mut Self> {
let offset = self.cursor % 8;
self.try_offset(offset)
}
sourcepub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self
pub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self
Advance the cursor by offset
bytes while explicitly setting
skipped bytes to zero. This can be useful if manually positioning
cursor within the buffer and the repositioning can result in
the buffer containing previously serialized data.
sourcepub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self>
pub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self>
Try advance the cursor by offset
bytes while explicitly setting
skipped bytes to zero. This can be useful if manually positioning
cursor within the buffer and the repositioning can result in
the buffer containing previously serialized data.
sourcepub fn align_u32(&mut self) -> &mut Self
pub fn align_u32(&mut self) -> &mut Self
Advance the cursor to ensure that the current cursor position is on the 32-bit alignment boundary.
sourcepub fn try_align_u32(&mut self) -> Result<&mut Self>
pub fn try_align_u32(&mut self) -> Result<&mut Self>
Try advance the cursor to ensure that the current cursor position is on the 32-bit alignment boundary.
sourcepub fn align_u64(&mut self) -> &mut Self
pub fn align_u64(&mut self) -> &mut Self
Advance the cursor to ensure that the current cursor position is on the 64-bit alignment boundary.
sourcepub fn try_align_u64(&mut self) -> Result<&mut Self>
pub fn try_align_u64(&mut self) -> Result<&mut Self>
Try advance the cursor to ensure that the current cursor position is on the 64-bit alignment boundary.
sourcepub fn store_u8(&mut self, v: u8) -> &mut Self
pub fn store_u8(&mut self, v: u8) -> &mut Self
Store a single u8
value, advancing the cursor by 1 byte.
Examples found in repository?
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
pub fn offset_with_zeros(&mut self, offset: usize) -> &mut Self {
for _ in 0..offset {
self.store_u8(0);
}
self
}
/// Try advance the cursor by `offset` bytes while explicitly setting
/// skipped bytes to zero. This can be useful if manually positioning
/// cursor within the buffer and the repositioning can result in
/// the buffer containing previously serialized data.
pub fn try_offset_with_zeros(&mut self, offset: usize) -> Result<&mut Self> {
if self.cursor + offset >= self.data.len() {
return Err(Error::TryOffsetError(offset,self.cursor,self.len()));
}
for _ in 0..offset {
self.store_u8(0);
}
Ok(self)
}
sourcepub fn try_store_u8(&mut self, v: u8) -> Result<&mut Self>
pub fn try_store_u8(&mut self, v: u8) -> Result<&mut Self>
Try store a single u8
value, advancing the cursor by 1 byte.
sourcepub fn store_u16le(&mut self, v: u16) -> &mut Self
pub fn store_u16le(&mut self, v: u16) -> &mut Self
Store a u16
value using little-endian encoding, advancing the cursor by 2 bytes.
sourcepub fn try_store_u16le(&mut self, v: u16) -> Result<&mut Self>
pub fn try_store_u16le(&mut self, v: u16) -> Result<&mut Self>
Try to store a u16
value using little-endian encoding, advancing the cursor by 2 bytes.
sourcepub fn store_u32le(&mut self, v: u32) -> &mut Self
pub fn store_u32le(&mut self, v: u32) -> &mut Self
Store a u32
value using little-endian encoding, advancing the cursor by 4 bytes.
sourcepub fn try_store_u32le(&mut self, v: u32) -> Result<&mut Self>
pub fn try_store_u32le(&mut self, v: u32) -> Result<&mut Self>
Try to store a u32
value using little-endian encoding, advancing the cursor by 4 bytes.
sourcepub fn store_u64le(&mut self, v: u64) -> &mut Self
pub fn store_u64le(&mut self, v: u64) -> &mut Self
Store a u64
value using little-endian encoding, advancing the cursor by 8 bytes.
sourcepub fn try_store_u64le(&mut self, v: u64) -> Result<&mut Self>
pub fn try_store_u64le(&mut self, v: u64) -> Result<&mut Self>
Try to store a u64
value using little-endian encoding, advancing the cursor by 8 bytes.
sourcepub fn try_store_utf16le_sz(&mut self, text: &String) -> Result<&mut Self>
pub fn try_store_utf16le_sz(&mut self, text: &String) -> Result<&mut Self>
Try to store a Rust String
as a zero-terminated sequence of u16
little-endian encoded bytes. This is useful to serialize windows PCWSTR
zero-terminated strings.
sourcepub fn try_store_u8_slice(&mut self, vec: &[u8]) -> Result<&mut Self>
pub fn try_store_u8_slice(&mut self, vec: &[u8]) -> Result<&mut Self>
Try to store a u8
slice
sourcepub fn try_store_u16le_slice(&mut self, vec: &[u16]) -> Result<&mut Self>
pub fn try_store_u16le_slice(&mut self, vec: &[u16]) -> Result<&mut Self>
Try to store a u16
slice as a sequence of little-endian encoded u16
values.
Examples found in repository?
452 453 454 455 456 457 458 459 460 461 462 463 464 465
pub fn try_store_utf16le_sz(&mut self, text : &String) -> Result<&mut Self> {
let len = text.len()+1;
let mut vec: Vec<u16> = Vec::with_capacity(len);
for c in text.chars() {
// TODO - proper encoding
// let buf = [0;2];
// c.encode_utf16(&mut buf);
vec.push(c as u16);
}
vec.push(0);
// println!("text: {} vec: {:?}",text,vec);
self.try_store_u16le_slice(&vec)?;
Ok(self)
}
sourcepub fn store<S: Serialize>(&mut self, s: &S) -> &mut Self
pub fn store<S: Serialize>(&mut self, s: &S) -> &mut Self
Store a primitive implementing a Serialize
trait
sourcepub fn try_store<S: TrySerialize>(
&mut self,
s: &S
) -> Result<&mut Self, S::Error>
pub fn try_store<S: TrySerialize>(
&mut self,
s: &S
) -> Result<&mut Self, S::Error>
Try store a primitive implementing a TrySerialize
trait
Trait Implementations§
source§impl Default for Serializer
impl Default for Serializer
Default implementation for Serializer
that allocates 4096 byte buffer.