Skip to main content

irox_bits/
seek.rs

1// SPDX-License-Identifier: MIT
2// Copyright 2025 IROX Contributors
3//
4use crate::Error;
5
6/// Enum to indicate how to move a read/write pointer.
7#[derive(Debug, Copy, Clone, Eq, PartialEq)]
8pub enum SeekFrom {
9    Start(u64),
10    End(i64),
11    Current(i64),
12}
13
14cfg_feature_std! {
15    impl From<SeekFrom> for std::io::SeekFrom {
16        fn from(fr: SeekFrom) -> Self {
17            match fr {
18                SeekFrom::Start(n) => std::io::SeekFrom::Start(n),
19                SeekFrom::End(n) => std::io::SeekFrom::End(n),
20                SeekFrom::Current(n) => std::io::SeekFrom::Current(n),
21            }
22        }
23    }
24}
25
26///
27/// Trait to move the current read/write position of a stream.  
28///
29/// There's no reason this trait should have been in std and not core.  
30pub trait Seek {
31    fn seek(&mut self, pos: SeekFrom) -> Result<u64, Error>;
32    fn position(&mut self) -> Result<u64, Error>;
33
34    fn rewind(&mut self) -> Result<(), Error> {
35        self.seek(SeekFrom::Start(0))?;
36        Ok(())
37    }
38    fn stream_len(&mut self) -> Result<u64, Error> {
39        let old_pos = self.stream_position()?;
40        let len = self.seek(SeekFrom::End(0))?;
41        if old_pos != len {
42            self.seek(SeekFrom::Start(old_pos))?;
43        }
44
45        Ok(len)
46    }
47
48    fn stream_position(&mut self) -> Result<u64, Error> {
49        self.seek(SeekFrom::Current(0))
50    }
51}
52
53///
54/// Trait to permit an atomic Seek+Read operation
55pub trait SeekRead {
56    /// Seek to the offset, relative to the start of the stream, and read some bytes into the
57    /// provided buffer.  The number of bytes read is returned.
58    fn seek_read(&mut self, out: &mut [u8], offset: u64) -> Result<usize, Error>;
59
60    /// Reads all data from the offset into the provided buffer.  May require multiple file ops &
61    /// seeks if the calls do not read all the data.
62    fn seek_read_all(&mut self, mut out: &mut [u8], mut offset: u64) -> Result<(), Error> {
63        while !out.is_empty() {
64            let split = self.seek_read(out, offset)?;
65            let (_, newo) = out.split_at_mut(split);
66            out = newo;
67            offset += split as u64;
68        }
69        Ok(())
70    }
71}
72
73///
74/// Trait to permit an atomic Seek+Write operation
75pub trait SeekWrite {
76    /// Seek to the provided offset, relative to the start of the stream, and write some bytes
77    /// from the provided buffer.  The number of bytes written is returned.
78    fn seek_write(&mut self, input: &[u8], offset: u64) -> Result<usize, Error>;
79
80    /// Writes all the data to the provided offset.  May require multiple file ops & seeks
81    /// if the calls do not write all the data.
82    fn seek_write_all(&mut self, mut input: &[u8], mut offset: u64) -> Result<(), Error> {
83        while !input.is_empty() {
84            let split = self.seek_write(input, offset)?;
85            let (_, nxt) = input.split_at(split);
86            offset += split as u64;
87            input = nxt;
88        }
89        Ok(())
90    }
91}