use crate::api::FileHandle;
#[bitfields::bitfield(u8)]
#[derive(PartialEq, Eq)]
struct WriteFlags {
write_all: bool,
#[bits(7)]
_reserved: u8,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct WriteOptions {
flags: WriteFlags,
}
impl WriteOptions {
pub const fn new() -> Self {
Self {
flags: WriteFlags::new(),
}
}
pub const fn write_all(mut self, complete: bool) -> Self {
self.flags.set_write_all(complete);
self
}
pub const fn writes_all(self) -> bool {
self.flags.write_all()
}
}
pub struct WriteOp<'a, H: FileHandle + 'a> {
file: &'a H,
offset: u64,
data: &'a [u8],
}
impl<H: FileHandle> Copy for WriteOp<'_, H> {}
impl<H: FileHandle> Clone for WriteOp<'_, H> {
fn clone(&self) -> Self {
*self
}
}
impl<H: FileHandle> std::fmt::Debug for WriteOp<'_, H> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WriteOp")
.field("offset", &self.offset)
.field("length", &self.data.len())
.finish_non_exhaustive()
}
}
impl<'a, H: FileHandle> WriteOp<'a, H> {
pub fn at(file: &'a H, offset: u64, data: &'a [u8]) -> Self {
Self { file, offset, data }
}
pub fn file(&self) -> &'a H {
self.file
}
pub fn offset(&self) -> u64 {
self.offset
}
pub fn data(&self) -> &'a [u8] {
self.data
}
}
#[cfg(test)]
mod option_layout_tests {
use super::*;
#[test]
fn packed_completion_flag_keeps_const_defaults_and_toggles() {
const COMPLETE: WriteOptions = WriteOptions::new().write_all(true);
assert_eq!(std::mem::size_of::<WriteOptions>(), 1);
assert!(!WriteOptions::default().writes_all());
assert!(COMPLETE.writes_all());
assert!(!COMPLETE.write_all(false).writes_all());
assert!(COMPLETE.write_all(false).write_all(true).writes_all());
}
}