#[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()
}
}
#[repr(transparent)]
pub struct WriteOp<'a, H>(crate::internal::WriteRequest<&'a H, &'a [u8], u64, ()>);
impl<H> Copy for WriteOp<'_, H> {}
impl<H> Clone for WriteOp<'_, H> {
fn clone(&self) -> Self {
*self
}
}
impl<H> std::fmt::Debug for WriteOp<'_, H> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<'a, H> WriteOp<'a, H> {
pub fn new(file: &'a H, offset: u64, data: &'a [u8]) -> Self {
Self(crate::internal::WriteRequest::new(file, offset, data))
}
pub fn at(file: &'a H, offset: u64, data: &'a [u8]) -> Self {
Self::new(file, offset, data)
}
pub fn file(&self) -> &'a H {
self.0.file()
}
pub fn offset(&self) -> u64 {
self.0.offset()
}
pub fn data(&self) -> &'a [u8] {
self.0.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());
}
}