Skip to content

Commit 97ef13b

Browse files
authored
Merge pull request #13 from dronesforwork-forks/expose-inner
Expose the underlying file object.
2 parents 43d9d5f + 9e7c0fe commit 97ef13b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/lib.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,34 @@ impl SpidevOptions {
206206
}
207207

208208
impl Spidev {
209+
/// Wrap an already opened [`File`] for use as an spidev
210+
pub fn new(devfile: File) -> Self {
211+
Self { devfile }
212+
}
213+
214+
209215
/// Open the spidev device with the provided path
210216
///
211217
/// Typically, the path will be something like `"/dev/spidev0.0"`
212218
/// where the first number if the bus and the second number
213219
/// is the chip select on that bus for the device being targeted.
214220
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Spidev> {
215-
let devfile = try!(OpenOptions::new()
216-
.read(true)
217-
.write(true)
218-
.create(false)
219-
.open(path));
220-
Ok(Spidev { devfile: devfile })
221+
let devfile = OpenOptions::new()
222+
.read(true)
223+
.write(true)
224+
.create(false)
225+
.open(path)?;
226+
Ok(Self::new(devfile))
227+
}
228+
229+
/// Get a reference to the underlying [`File`] object
230+
pub fn inner(&self) -> &File {
231+
&self.devfile
232+
}
233+
234+
/// Consume the object and get the underlying [`File`] object
235+
pub fn into_inner(self) -> File {
236+
self.devfile
221237
}
222238

223239
/// Write the provided configuration to this device

0 commit comments

Comments
 (0)