Skip to main content

vfsi_core/api/
write.rs

1//! Portable borrowed positional writes.
2
3#[bitfields::bitfield(u8)]
4#[derive(PartialEq, Eq)]
5struct WriteFlags {
6    write_all: bool,
7    #[bits(7)]
8    _reserved: u8,
9}
10
11/// Policy for positional vector writes. Defaults to reporting short writes.
12/// Completion is not atomicity, durability, or permission to replay failed RPCs.
13#[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    /// Report each request's accepted byte count without completing short writes.
20    pub const fn new() -> Self {
21        Self {
22            flags: WriteFlags::new(),
23        }
24    }
25    /// Complete successful short writes at their remaining offsets when true.
26    /// Stop on any error, including ambiguous transport failures; never replay it.
27    /// This does not flush data to stable storage or make the batch atomic.
28    pub const fn with_write_all(mut self, complete: bool) -> Self {
29        self.flags.set_write_all(complete);
30        self
31    }
32    /// Whether successful short writes should be completed.
33    pub const fn write_all(self) -> bool {
34        self.flags.write_all()
35    }
36}
37
38/// A positional write borrowing its handle and payload without copying either.
39/// Construction performs no I/O or allocation. Does not change the file cursor.
40/// Uses the same storage definition as the native [`crate::WriteOp`].
41#[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    /// Borrow a handle and payload at an absolute byte offset.
58    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}