Skip to content

Commit fb329e8

Browse files
authored
Merge pull request #730 from ncaracci/whence
Documentation for Whence.
2 parents 5a54f9f + 3d7d7ea commit fb329e8

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

src/unistd.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -737,36 +737,46 @@ pub fn write(fd: RawFd, buf: &[u8]) -> Result<usize> {
737737
Errno::result(res).map(|r| r as usize)
738738
}
739739

740+
/// Directive that tells [`lseek`] and [`lseek64`] what the offset is relative to.
741+
/// [`lseek`]: ./fn.lseek.html
742+
/// [`lseek64`]: ./fn.lseek64.html
743+
#[repr(i32)]
740744
pub enum Whence {
741-
SeekSet,
742-
SeekCur,
743-
SeekEnd,
744-
SeekData,
745-
SeekHole
746-
}
747-
748-
impl Whence {
749-
fn to_libc_type(&self) -> c_int {
750-
match self {
751-
&Whence::SeekSet => libc::SEEK_SET,
752-
&Whence::SeekCur => libc::SEEK_CUR,
753-
&Whence::SeekEnd => libc::SEEK_END,
754-
&Whence::SeekData => 3,
755-
&Whence::SeekHole => 4
756-
}
757-
}
758-
745+
/// Specify an offset relative to the start of the file.
746+
SeekSet = libc::SEEK_SET,
747+
/// Specify an offset relative to the current file location.
748+
SeekCur = libc::SEEK_CUR,
749+
/// Specify an offset relative to the end of the file.
750+
SeekEnd = libc::SEEK_END,
751+
/// Specify an offset relative to the next location in the file greater than or
752+
/// equal to offset that contains some data. If offset points to
753+
/// some data, then the file offset is set to offset.
754+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
755+
all(target_os = "linux", not(any(target_env = "musl",
756+
target_arch = "mips",
757+
target_arch = "mips64")))))]
758+
SeekData = libc::SEEK_DATA,
759+
/// Specify an offset relative to the next hole in the file greater than
760+
/// or equal to offset. If offset points into the middle of a hole, then
761+
/// the file offset should be set to offset. If there is no hole past offset,
762+
/// then the file offset should be adjusted to the end of the file (i.e., there
763+
/// is an implicit hole at the end of any file).
764+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd",
765+
all(target_os = "linux", not(any(target_env = "musl",
766+
target_arch = "mips",
767+
target_arch = "mips64")))))]
768+
SeekHole = libc::SEEK_HOLE
759769
}
760770

761771
pub fn lseek(fd: RawFd, offset: libc::off_t, whence: Whence) -> Result<libc::off_t> {
762-
let res = unsafe { libc::lseek(fd, offset, whence.to_libc_type()) };
772+
let res = unsafe { libc::lseek(fd, offset, whence as i32) };
763773

764774
Errno::result(res).map(|r| r as libc::off_t)
765775
}
766776

767777
#[cfg(any(target_os = "linux", target_os = "android"))]
768778
pub fn lseek64(fd: RawFd, offset: libc::off64_t, whence: Whence) -> Result<libc::off64_t> {
769-
let res = unsafe { libc::lseek64(fd, offset, whence.to_libc_type()) };
779+
let res = unsafe { libc::lseek64(fd, offset, whence as i32) };
770780

771781
Errno::result(res).map(|r| r as libc::off64_t)
772782
}

0 commit comments

Comments
 (0)