Skip to content

Commit 13cbae0

Browse files
committed
Added documentation to Whence.
Changed SeekData and SeekHole hardcoded values to libc ones and added availability only to linux freebsd and dragonflybsd. Removed impl Whence and assigned the libc values directly in the enum definition.
1 parent 087aece commit 13cbae0

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

src/unistd.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -737,36 +737,40 @@ 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", target_os = "linux"))]
755+
SeekData = libc::SEEK_DATA,
756+
/// Specify an offset relative to the next hole in the file greater than
757+
/// or equal to offset. If offset points into the middle of a hole, then
758+
/// the file offset should be set to offset. If there is no hole past offset,
759+
/// then the file offset should be adjusted to the end of the file (i.e., there
760+
/// is an implicit hole at the end of any file).
761+
#[cfg(any(target_os = "dragonfly", target_os = "freebsd", target_os = "linux"))]
762+
SeekHole = libc::SEEK_HOLE
759763
}
760764

761765
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()) };
766+
let res = unsafe { libc::lseek(fd, offset, whence as i32) };
763767

764768
Errno::result(res).map(|r| r as libc::off_t)
765769
}
766770

767771
#[cfg(any(target_os = "linux", target_os = "android"))]
768772
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()) };
773+
let res = unsafe { libc::lseek64(fd, offset, whence as i32) };
770774

771775
Errno::result(res).map(|r| r as libc::off64_t)
772776
}

0 commit comments

Comments
 (0)