This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Streamer #57
Merged
Merged
Streamer #57
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| use serde_json; | ||
| use std; | ||
| use std::any::Any; | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum Error { | ||
| IO(std::io::Error), | ||
| JSON(serde_json::Error), | ||
| AddrParse(std::net::AddrParseError), | ||
| JoinError(Box<Any + Send + 'static>), | ||
| RecvError(std::sync::mpsc::RecvError), | ||
| RecvTimeoutError(std::sync::mpsc::RecvTimeoutError), | ||
| SendError, | ||
| } | ||
|
|
||
| pub type Result<T> = std::result::Result<T, Error>; | ||
|
|
||
| impl std::convert::From<std::sync::mpsc::RecvError> for Error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool way to do it. |
||
| fn from(e: std::sync::mpsc::RecvError) -> Error { | ||
| Error::RecvError(e) | ||
| } | ||
| } | ||
| impl std::convert::From<std::sync::mpsc::RecvTimeoutError> for Error { | ||
| fn from(e: std::sync::mpsc::RecvTimeoutError) -> Error { | ||
| Error::RecvTimeoutError(e) | ||
| } | ||
| } | ||
| impl<T> std::convert::From<std::sync::mpsc::SendError<T>> for Error { | ||
| fn from(_e: std::sync::mpsc::SendError<T>) -> Error { | ||
| Error::SendError | ||
| } | ||
| } | ||
| impl std::convert::From<Box<Any + Send + 'static>> for Error { | ||
| fn from(e: Box<Any + Send + 'static>) -> Error { | ||
| Error::JoinError(e) | ||
| } | ||
| } | ||
|
|
||
| impl std::convert::From<std::io::Error> for Error { | ||
| fn from(e: std::io::Error) -> Error { | ||
| Error::IO(e) | ||
| } | ||
| } | ||
| impl std::convert::From<serde_json::Error> for Error { | ||
| fn from(e: serde_json::Error) -> Error { | ||
| Error::JSON(e) | ||
| } | ||
| } | ||
| impl std::convert::From<std::net::AddrParseError> for Error { | ||
| fn from(e: std::net::AddrParseError) -> Error { | ||
| Error::AddrParse(e) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use result::Result; | ||
| use result::Error; | ||
| use std::net::SocketAddr; | ||
| use std::sync::mpsc::RecvError; | ||
| use std::sync::mpsc::RecvTimeoutError; | ||
| use std::thread; | ||
| use std::io; | ||
| use std::io::Write; | ||
| use serde_json; | ||
| use std::sync::mpsc::channel; | ||
|
|
||
| fn addr_parse_error() -> Result<SocketAddr> { | ||
| let r = "12fdfasfsafsadfs".parse()?; | ||
| Ok(r) | ||
| } | ||
|
|
||
| fn join_error() -> Result<()> { | ||
| let r = thread::spawn(|| panic!("hi")).join()?; | ||
| Ok(r) | ||
| } | ||
| fn json_error() -> Result<()> { | ||
| let r = serde_json::from_slice("=342{;;;;:}".as_bytes())?; | ||
| Ok(r) | ||
| } | ||
| fn send_error() -> Result<()> { | ||
| let (s, r) = channel(); | ||
| drop(r); | ||
| s.send(())?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn from_test() { | ||
| assert_matches!(addr_parse_error(), Err(Error::AddrParse(_))); | ||
| assert_matches!(Error::from(RecvError {}), Error::RecvError(_)); | ||
| assert_matches!( | ||
| Error::from(RecvTimeoutError::Timeout), | ||
| Error::RecvTimeoutError(_) | ||
| ); | ||
| assert_matches!(send_error(), Err(Error::SendError)); | ||
| assert_matches!(join_error(), Err(Error::JoinError(_))); | ||
| let ioe = io::Error::new(io::ErrorKind::NotFound, "hi"); | ||
| assert_matches!(Error::from(ioe), Error::IO(_)); | ||
| } | ||
| #[test] | ||
| fn fmt_test() { | ||
| write!(io::sink(), "{:?}", addr_parse_error()).unwrap(); | ||
| write!(io::sink(), "{:?}", Error::from(RecvError {})).unwrap(); | ||
| write!(io::sink(), "{:?}", Error::from(RecvTimeoutError::Timeout)).unwrap(); | ||
| write!(io::sink(), "{:?}", send_error()).unwrap(); | ||
| write!(io::sink(), "{:?}", join_error()).unwrap(); | ||
| write!(io::sink(), "{:?}", json_error()).unwrap(); | ||
| write!( | ||
| io::sink(), | ||
| "{:?}", | ||
| Error::from(io::Error::new(io::ErrorKind::NotFound, "hi")) | ||
| ).unwrap(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems odd. What's going on here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am converting the join error type into the result type. It has a really weird signature because its a return value of the closure that is passed into spawn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That `static most likely comes from the possibility of the thread outliving the parent thread. You can get that out of there by either using an explicit lifetime or using scoped threads from crossbeam.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like a big change just for the type. for a return value the 'static' isn't a huge problem because all-most all the apis just return ()