From 0351857606ea91ebe9c1884d8e5418b9689b7f69 Mon Sep 17 00:00:00 2001 From: Josh Mitchell Date: Sun, 8 Nov 2020 00:43:29 +1100 Subject: [PATCH] Added open_read, open_write and open_append convenience methods --- src/lib.rs | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 83eab0d..be9c529 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,8 @@ //! use xdrfile::*; //! use std::path::Path; //! -//! let mut path = Path::new("tests/1l2y.xtc"); //! // get a handle to the file -//! let mut trj = XTCTrajectory::open(path, FileMode::Read).unwrap(); +//! let mut trj = XTCTrajectory::open_read("tests/1l2y.xtc").unwrap(); //! //! // find number of atoms in the file //! let num_atoms = trj.get_num_atoms().unwrap(); @@ -220,6 +219,21 @@ impl XTCTrajectory { num_atoms: Lazy::new(), }) } + + /// Open a file in read mode + pub fn open_read(path: impl AsRef) -> Result { + Self::open(path, FileMode::Read) + } + + /// Open a file in append mode + pub fn open_append(path: impl AsRef) -> Result { + Self::open(path, FileMode::Append) + } + + /// Open a file in write mode + pub fn open_write(path: impl AsRef) -> Result { + Self::open(path, FileMode::Write) + } } impl Trajectory for XTCTrajectory { @@ -307,6 +321,21 @@ impl TRRTrajectory { num_atoms: Lazy::new(), }) } + + /// Open a file in read mode + pub fn open_read(path: impl AsRef) -> Result { + Self::open(path, FileMode::Read) + } + + /// Open a file in append mode + pub fn open_append(path: impl AsRef) -> Result { + Self::open(path, FileMode::Append) + } + + /// Open a file in write mode + pub fn open_write(path: impl AsRef) -> Result { + Self::open(path, FileMode::Write) + } } impl Trajectory for TRRTrajectory {