impl<T> MemoryWriter<T> where
T: TryIntoCtx<scroll::Endian, Error = scroll::Error> + SizeWith<scroll::Endian>,
{ /// Create a slot for a type T in the buffer, we can fill right now with real values. pubfn alloc_with_val(buffer: &mut Buffer, val: T) -> WriteResult<Self> { // Mark the position as we may overwrite later let position = buffer.position(); let size = buffer.write(val)?;
Ok(Self {
position: position as u32,
size,
phantom: std::marker::PhantomData,
})
}
/// Create a slot for a type T in the buffer, we can fill later with real values. pubfn alloc(buffer: &mut Buffer) -> WriteResult<Self> { let size = size!(T); let position = buffer.reserve(size) as u32;
/// Write actual values in the buffer-slot we got during `alloc()` #[inline] pubfn set_value(&mutself, buffer: &mut Buffer, val: T) -> WriteResult<()> {
Ok(buffer.write_at(self.position as usize, val).map(|_sz| ())?)
}
Self {
position: position as u32,
array_size: slice.len(),
phantom: std::marker::PhantomData,
}
}
}
impl<T> MemoryArrayWriter<T> where
T: TryIntoCtx<scroll::Endian, Error = scroll::Error> + SizeWith<scroll::Endian> + Copy,
{ pubfn alloc_from_array(buffer: &mut Buffer, array: &[T]) -> WriteResult<Self> { let array_size = array.len(); let position = buffer.reserve(array_size * size!(T));
for (idx, val) in array.iter().enumerate() {
buffer.write_at(position + idx * size!(T), *val)?;
}
Ok(Self {
position: position as u32,
array_size,
phantom: std::marker::PhantomData,
})
}
}
impl<T> MemoryArrayWriter<T> where
T: TryIntoCtx<scroll::Endian, Error = scroll::Error> + SizeWith<scroll::Endian>,
{ /// Create a slot for a type T in the buffer, we can fill in the values in one go. pubfn alloc_from_iter<I>(
buffer: &mut Buffer,
iter: impl IntoIterator<Item = T, IntoIter = I>,
) -> WriteResult<Self> where
I: std::iter::ExactSizeIterator<Item = T>,
{ let iter = iter.into_iter(); let array_size = iter.len(); let size = size!(T); let position = buffer.reserve(array_size * size);
for (idx, val) in iter.enumerate() {
buffer.write_at(position + idx * size, val)?;
}
Ok(Self {
position: position as u32,
array_size,
phantom: std::marker::PhantomData,
})
}
/// Create a slot for a type T in the buffer, we can fill later with real values. /// This function fills it with `Default::default()`, which is less performant than /// using uninitialized memory, but safe. pubfn alloc_array(buffer: &mut Buffer, array_size: usize) -> WriteResult<Self> { let position = buffer.reserve(array_size * size!(T));
Ok(Self {
position: position as u32,
array_size,
phantom: std::marker::PhantomData,
})
}
/// Write actual values in the buffer-slot we got during `alloc()` #[inline] pubfn set_value_at(&mutself, buffer: &mut Buffer, val: T, index: usize) -> WriteResult<()> {
Ok(buffer
.write_at(self.position as usize + size!(T) * index, val)
.map(|_sz| ())?)
}
// First write size of the string (x letters in u16, times the size of u16) let text_header = MemoryWriter::<u32>::alloc_with_val(
buffer,
(letters.len() * std::mem::size_of::<u16>()).try_into()?,
)?;
// Then write utf-16 letters after that letmut text_section = MemoryArrayWriter::<u16>::alloc_array(buffer, letters.len())?; for (index, letter) in letters.iter().enumerate() {
text_section.set_value_at(buffer, *letter, index)?;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.