web_fs/
seek.rs

1use std::{
2    io::{Error, Result, SeekFrom},
3    pin::Pin,
4    task::{Context, Poll},
5};
6
7use futures_lite::AsyncSeek;
8
9use crate::File;
10
11const SEEK_ERROR: &str = "Move cursor to negative value";
12
13impl AsyncSeek for File {
14    /// File System API dosen't fully expose the cursor of the file, so this is a simulated one and does not actually require async.
15    fn poll_seek(
16        mut self: Pin<&mut Self>,
17        _cx: &mut Context<'_>,
18        pos: SeekFrom,
19    ) -> Poll<Result<u64>> {
20        match pos {
21            SeekFrom::Current(offset) => {
22                self.cursor = self
23                    .cursor
24                    .checked_add_signed(offset)
25                    .ok_or(Error::other(SEEK_ERROR))?
26            }
27            SeekFrom::End(offset) => {
28                self.cursor = self
29                    .size
30                    .checked_add_signed(offset)
31                    .ok_or(Error::other(SEEK_ERROR))?
32            }
33            SeekFrom::Start(offset) => self.cursor += offset,
34        }
35        Poll::Ready(Ok(self.cursor))
36    }
37}