pub struct AtomicWriter { /* private fields */ }Expand description
流式原子写入器。
适用于加密大文件——载荷逐块产生,不必先在内存里拼出完整文件。
未调用 AtomicWriter::commit 就 drop 时,临时文件会被自动删除。
§示例
use omy_core::fsatomic::AtomicWriter;
let mut w = AtomicWriter::create(std::path::Path::new("out.omy"))?;
w.write_all(b"header")?;
w.write_all(b"chunk-0")?;
w.commit()?; // 不调用 commit 则临时文件被丢弃Implementations§
Source§impl AtomicWriter
impl AtomicWriter
Sourcepub fn create(target: &Path) -> Result<Self>
pub fn create(target: &Path) -> Result<Self>
创建临时文件并准备写入。
临时文件名为 <target>.<pid>.<nonce><TMP_SUFFIX>,与目标同目录——
这是 rename 原子性的前提(跨文件系统的 rename 不是原子操作,且多数平台
直接失败)。
名字必须每个实例唯一,不能固定为 <target>.tmp:两个并发写入者(例如
两个 Tauri 命令线程同时保存同一配置)会共用同一个 tmp 文件,先提交者
的 rename 会把 tmp 移走,后提交者的 rename 报 ENOENT——并发保存因此
偶发失败。唯一的 tmp 让各写入者互不干扰,最终语义仍是「最后一次提交
胜出」。
§Errors
创建目录或临时文件失败时返回 Error::Io。
Trait Implementations§
Source§impl Debug for AtomicWriter
impl Debug for AtomicWriter
Source§impl Drop for AtomicWriter
impl Drop for AtomicWriter
Source§impl Seek for AtomicWriter
标准 Seek:流式加密写完载荷后要回到开头填写头部。
impl Seek for AtomicWriter
标准 Seek:流式加密写完载荷后要回到开头填写头部。
注意 written 统计的是写入调用累计的字节数,seek 回去覆写头部会让它
大于文件长度;需要文件长度时以 stream_position / 元数据为准。
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Rewind to the beginning of a stream. Read more
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
🔬This is a nightly-only experimental API. (
seek_stream_len)Returns the length of this stream (in bytes). Read more
Source§impl Write for AtomicWriter
标准 Write:让流式加密直接写进临时文件,提交前目标位置不受影响。
impl Write for AtomicWriter
标准 Write:让流式加密直接写进临时文件,提交前目标位置不受影响。
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Writes a buffer into this writer, returning how many bytes were written. Read more
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flushes this output stream, ensuring that all intermediately buffered
contents reach their destination. Read more
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
🔬This is a nightly-only experimental API. (
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
🔬This is a nightly-only experimental API. (
write_all_vectored)Attempts to write multiple buffers into this writer. Read more
Auto Trait Implementations§
impl Forget for AtomicWriter
impl Freeze for AtomicWriter
impl RefUnwindSafe for AtomicWriter
impl Send for AtomicWriter
impl Sync for AtomicWriter
impl Unpin for AtomicWriter
impl UnsafeUnpin for AtomicWriter
impl UnwindSafe for AtomicWriter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more