Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ codecov = { repository = "loomprotocol/silk", branch = "master", service = "gith

[features]
unstable = []
ipv6 = []
asm = ["sha2-asm"]

[dependencies]
Expand All @@ -54,3 +55,5 @@ ring = "0.12.1"
untrusted = "0.5.1"
bincode = "1.0.0"
chrono = { version = "0.4.0", features = ["serde"] }
log = "^0.4.1"
matches = "^0.1.6"
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ pub mod log;
pub mod mint;
pub mod logger;
pub mod historian;
pub mod streamer;
pub mod accountant;
pub mod accountant_skel;
pub mod accountant_stub;
pub mod result;
extern crate bincode;
extern crate chrono;
extern crate generic_array;
#[macro_use]
extern crate log as logging;
extern crate rayon;
extern crate ring;
extern crate serde;
Expand All @@ -22,3 +26,7 @@ extern crate serde_derive;
extern crate serde_json;
extern crate sha2;
extern crate untrusted;

#[cfg(test)]
#[macro_use]
extern crate matches;
115 changes: 115 additions & 0 deletions src/result.rs
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>),
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Member Author

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.

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Member Author

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 ()

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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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();
}
}
Loading