Skip to content

Commit

Permalink
Note why Handle methods in win and unix won't panic
Browse files Browse the repository at this point in the history
Fixes #7
Fixes #8
  • Loading branch information
yandexx authored and BurntSushi committed Aug 3, 2017
1 parent 6660cb0 commit 8c09246
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/unix.rs
Expand Up @@ -18,6 +18,8 @@ pub struct Handle {
impl Drop for Handle {
fn drop(&mut self) {
if self.is_std {
// unwrap() will not panic. Since we were able to open an
// std stream successfully, then `file` is guaranteed to be Some()
self.file.take().unwrap().into_raw_fd();
}
}
Expand All @@ -33,12 +35,16 @@ impl PartialEq for Handle {

impl AsRawFd for ::Handle {
fn as_raw_fd(&self) -> RawFd {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.0.file.as_ref().take().unwrap().as_raw_fd()
}
}

impl IntoRawFd for ::Handle {
fn into_raw_fd(mut self) -> RawFd {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.0.file.take().unwrap().into_raw_fd()
}
}
Expand Down Expand Up @@ -85,10 +91,14 @@ impl Handle {
}

pub fn as_file(&self) -> &File {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.file.as_ref().take().unwrap()
}

pub fn as_file_mut(&mut self) -> &mut File {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.file.as_mut().take().unwrap()
}

Expand Down
10 changes: 10 additions & 0 deletions src/win.rs
Expand Up @@ -76,6 +76,8 @@ struct Key {
impl Drop for Handle {
fn drop(&mut self) {
if self.is_std {
// unwrap() will not panic. Since we were able to open an
// std stream successfully, then `file` is guaranteed to be Some()
self.file.take().unwrap().into_raw_handle();
}
}
Expand All @@ -94,12 +96,16 @@ impl PartialEq for Handle {

impl AsRawHandle for ::Handle {
fn as_raw_handle(&self) -> RawHandle {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.0.file.as_ref().take().unwrap().as_raw_handle()
}
}

impl IntoRawHandle for ::Handle {
fn into_raw_handle(mut self) -> RawHandle {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.0.file.take().unwrap().into_raw_handle()
}
}
Expand Down Expand Up @@ -171,10 +177,14 @@ impl Handle {
}

pub fn as_file(&self) -> &File {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.file.as_ref().take().unwrap()
}

pub fn as_file_mut(&mut self) -> &mut File {
// unwrap() will not panic. Since we were able to open the
// file successfully, then `file` is guaranteed to be Some()
self.file.as_mut().take().unwrap()
}
}
Expand Down

0 comments on commit 8c09246

Please sign in to comment.