linux_io/
seek.rs

1#[cfg(feature = "std")]
2extern crate std;
3
4#[allow(unused_imports)] // this is only for doc comments
5use crate::File;
6
7/// Used with [`File::seek`] to specify the starting point and offset.
8///
9/// This is just a copy of `std::io::SeekFrom`, included here to allow this
10/// crate to work in `no_std` environments. If this crate's `std` feature
11/// is enabled then `SeekFrom` can convert to and from `std::io::SeekFrom`.
12#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13pub enum SeekFrom {
14    Start(u64),
15    End(i64),
16    Current(i64),
17}
18
19impl SeekFrom {
20    #[inline]
21    pub const fn for_raw_offset(self) -> linux_unsafe::loff_t {
22        match self {
23            SeekFrom::Start(v) => v as linux_unsafe::loff_t,
24            SeekFrom::End(v) => v as linux_unsafe::loff_t,
25            SeekFrom::Current(v) => v as linux_unsafe::loff_t,
26        }
27    }
28
29    #[inline]
30    pub const fn for_raw_whence(self) -> linux_unsafe::int {
31        match self {
32            SeekFrom::Start(_) => linux_unsafe::SEEK_SET,
33            SeekFrom::End(_) => linux_unsafe::SEEK_END,
34            SeekFrom::Current(_) => linux_unsafe::SEEK_CUR,
35        }
36    }
37
38    #[allow(dead_code)] // only used on 32-bit platforms
39    #[inline]
40    pub const fn for_raw_uwhence(self) -> linux_unsafe::uint {
41        self.for_raw_whence() as linux_unsafe::uint
42    }
43}
44
45#[cfg(feature = "std")]
46impl From<std::io::SeekFrom> for SeekFrom {
47    fn from(value: std::io::SeekFrom) -> Self {
48        match value {
49            std::io::SeekFrom::Start(v) => Self::Start(v),
50            std::io::SeekFrom::End(v) => Self::End(v),
51            std::io::SeekFrom::Current(v) => Self::Current(v),
52        }
53    }
54}
55
56#[cfg(feature = "std")]
57impl Into<std::io::SeekFrom> for SeekFrom {
58    fn into(self) -> std::io::SeekFrom {
59        match self {
60            SeekFrom::Start(v) => std::io::SeekFrom::Start(v),
61            SeekFrom::End(v) => std::io::SeekFrom::End(v),
62            SeekFrom::Current(v) => std::io::SeekFrom::Current(v),
63        }
64    }
65}