mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-11 06:35:30 +00:00
Improved seek errors and implemented for TRR and XTC
This commit is contained in:
@@ -139,8 +139,10 @@ pub enum ErrorTask {
|
|||||||
Read,
|
Read,
|
||||||
/// A frame was being written to a file
|
/// A frame was being written to a file
|
||||||
Write,
|
Write,
|
||||||
/// An file was being flushed to disk
|
/// A file was being flushed to disk
|
||||||
Flush,
|
Flush,
|
||||||
|
/// A seek operation was being run on a file
|
||||||
|
Seek,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for ErrorTask {
|
impl std::fmt::Display for ErrorTask {
|
||||||
@@ -151,6 +153,7 @@ impl std::fmt::Display for ErrorTask {
|
|||||||
Read => write!(f, "reading trajectory"),
|
Read => write!(f, "reading trajectory"),
|
||||||
Write => write!(f, "writing trajectory"),
|
Write => write!(f, "writing trajectory"),
|
||||||
Flush => write!(f, "flushing trajectory"),
|
Flush => write!(f, "flushing trajectory"),
|
||||||
|
Seek => write!(f, "seeking in trajectory"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
53
src/lib.rs
53
src/lib.rs
@@ -175,13 +175,12 @@ impl io::Seek for XDRFile {
|
|||||||
End(i) => (2, i),
|
End(i) => (2, i),
|
||||||
};
|
};
|
||||||
unsafe {
|
unsafe {
|
||||||
let code = xdr_seek::xdr_seek(self.xdrfile, pos, whence);
|
let code = xdr_seek::xdr_seek(self.xdrfile, pos, whence) as u32;
|
||||||
if code as u32 != xdrfile::exdrOK {
|
match check_code(code, ErrorTask::Seek) {
|
||||||
return Err(io::Error::new(io::ErrorKind::Other, "Seek failed"));
|
None => Ok(self.tell()),
|
||||||
|
Some(err) => Err(io::Error::new(io::ErrorKind::Other, err)),
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
Ok(self.tell())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,6 +328,19 @@ impl Trajectory for XTCTrajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl XTCTrajectory {
|
||||||
|
/// Get the current position in the file
|
||||||
|
pub fn tell(&self) -> u64 {
|
||||||
|
self.handle.tell()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl io::Seek for XTCTrajectory {
|
||||||
|
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
||||||
|
self.handle.seek(pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Read/Write TRR Trajectories
|
/// Read/Write TRR Trajectories
|
||||||
pub struct TRRTrajectory {
|
pub struct TRRTrajectory {
|
||||||
handle: XDRFile,
|
handle: XDRFile,
|
||||||
@@ -452,6 +464,19 @@ impl Trajectory for TRRTrajectory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TRRTrajectory {
|
||||||
|
/// Get the current position in the file
|
||||||
|
pub fn tell(&self) -> u64 {
|
||||||
|
self.handle.tell()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl io::Seek for TRRTrajectory {
|
||||||
|
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
|
||||||
|
self.handle.seek(pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
@@ -617,17 +642,17 @@ mod tests {
|
|||||||
coords: vec![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]],
|
coords: vec![[0.0, 0.0, 0.0], [0.5, 0.5, 0.5]],
|
||||||
};
|
};
|
||||||
let mut f = TRRTrajectory::open_write(tmp_path)?;
|
let mut f = TRRTrajectory::open_write(tmp_path)?;
|
||||||
assert_eq!(f.handle.tell(), 0);
|
assert_eq!(f.tell(), 0);
|
||||||
f.write(&frame)?;
|
f.write(&frame)?;
|
||||||
assert_eq!(f.handle.tell(), 144);
|
assert_eq!(f.tell(), 144);
|
||||||
f.flush()?;
|
f.flush()?;
|
||||||
|
|
||||||
let mut new_frame = Frame::with_capacity(natoms);
|
let mut new_frame = Frame::with_capacity(natoms);
|
||||||
let mut f = TRRTrajectory::open_read(tmp_path)?;
|
let mut f = TRRTrajectory::open_read(tmp_path)?;
|
||||||
assert_eq!(f.handle.tell(), 0);
|
assert_eq!(f.tell(), 0);
|
||||||
|
|
||||||
f.read(&mut new_frame)?;
|
f.read(&mut new_frame)?;
|
||||||
assert_eq!(f.handle.tell(), 144);
|
assert_eq!(f.tell(), 144);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -647,21 +672,21 @@ mod tests {
|
|||||||
};
|
};
|
||||||
let mut f = TRRTrajectory::open_write(tmp_path)?;
|
let mut f = TRRTrajectory::open_write(tmp_path)?;
|
||||||
f.write(&frame)?;
|
f.write(&frame)?;
|
||||||
let after_first_frame = f.handle.tell();
|
let after_first_frame = f.tell();
|
||||||
frame.step += 1;
|
frame.step += 1;
|
||||||
frame.time += 10.0;
|
frame.time += 10.0;
|
||||||
f.write(&frame)?;
|
f.write(&frame)?;
|
||||||
let after_second_frame = f.handle.tell();
|
let after_second_frame = f.tell();
|
||||||
f.flush()?;
|
f.flush()?;
|
||||||
|
|
||||||
let mut new_frame = Frame::with_capacity(natoms);
|
let mut new_frame = Frame::with_capacity(natoms);
|
||||||
let mut f = TRRTrajectory::open_read(tmp_path)?;
|
let mut f = TRRTrajectory::open_read(tmp_path)?;
|
||||||
use std::io::Seek as _;
|
use std::io::Seek as _;
|
||||||
let pos = f.handle.seek(std::io::SeekFrom::Current(144))?;
|
let pos = f.seek(std::io::SeekFrom::Current(144))?;
|
||||||
assert_eq!(pos, after_first_frame);
|
assert_eq!(pos, after_first_frame);
|
||||||
|
|
||||||
f.read(&mut new_frame)?;
|
f.read(&mut new_frame)?;
|
||||||
assert_eq!(f.handle.tell(), after_second_frame);
|
assert_eq!(f.tell(), after_second_frame);
|
||||||
|
|
||||||
assert_eq!(new_frame.num_atoms, frame.num_atoms);
|
assert_eq!(new_frame.num_atoms, frame.num_atoms);
|
||||||
assert_eq!(new_frame.step, frame.step);
|
assert_eq!(new_frame.step, frame.step);
|
||||||
|
|||||||
Reference in New Issue
Block a user