1#[bitfields::bitfield(u8)]
4#[derive(PartialEq, Eq)]
5struct WriteFlags {
6 write_all: bool,
7 #[bits(7)]
8 _reserved: u8,
9}
10
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
14#[must_use = "configuration and requests have no effect until used"]
15pub struct WriteOptions {
16 flags: WriteFlags,
17}
18impl WriteOptions {
19 pub const fn new() -> Self {
21 Self {
22 flags: WriteFlags::new(),
23 }
24 }
25 pub const fn with_write_all(mut self, complete: bool) -> Self {
29 self.flags.set_write_all(complete);
30 self
31 }
32 pub const fn write_all(self) -> bool {
34 self.flags.write_all()
35 }
36}
37
38#[repr(transparent)]
42#[must_use = "configuration and requests have no effect until used"]
43pub struct WriteOp<'a, H>(crate::internal::WriteRequest<&'a H, &'a [u8], u64, ()>);
44
45impl<H> Copy for WriteOp<'_, H> {}
46impl<H> Clone for WriteOp<'_, H> {
47 fn clone(&self) -> Self {
48 *self
49 }
50}
51impl<H> std::fmt::Debug for WriteOp<'_, H> {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53 self.0.fmt(f)
54 }
55}
56impl<'a, H> WriteOp<'a, H> {
57 pub fn at(file: &'a H, offset: u64, data: &'a [u8]) -> Self {
59 Self(crate::internal::WriteRequest::new(file, offset, data))
60 }
61 pub fn file(&self) -> &'a H {
62 self.0.file()
63 }
64 pub fn offset(&self) -> u64 {
65 self.0.offset()
66 }
67 pub fn data(&self) -> &'a [u8] {
68 self.0.data()
69 }
70}
71
72#[cfg(test)]
73mod option_layout_tests {
74 use super::*;
75 #[test]
76 fn packed_completion_flag_keeps_const_defaults_and_toggles() {
77 const COMPLETE: WriteOptions = WriteOptions::new().with_write_all(true);
78 assert_eq!(std::mem::size_of::<WriteOptions>(), 1);
79 assert!(!WriteOptions::default().write_all());
80 assert!(COMPLETE.write_all());
81 assert!(!COMPLETE.with_write_all(false).write_all());
82 assert!(
83 COMPLETE
84 .with_write_all(false)
85 .with_write_all(true)
86 .write_all()
87 );
88 }
89}