diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:06:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:06:31 +0000 |
commit | 2ff14448863ac1a1dd9533461708e29aae170c2d (patch) | |
tree | 85b9fea2bbfe3f06473cfa381eed11f273b57c5c /vendor/quick-error/src/lib.rs | |
parent | Adding debian version 1.64.0+dfsg1-1. (diff) | |
download | rustc-2ff14448863ac1a1dd9533461708e29aae170c2d.tar.xz rustc-2ff14448863ac1a1dd9533461708e29aae170c2d.zip |
Adding debian version 1.65.0+dfsg1-2.debian/1.65.0+dfsg1-2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/quick-error/src/lib.rs')
-rw-r--r-- | vendor/quick-error/src/lib.rs | 292 |
1 files changed, 109 insertions, 183 deletions
diff --git a/vendor/quick-error/src/lib.rs b/vendor/quick-error/src/lib.rs index e5eb62e44..17fc455d4 100644 --- a/vendor/quick-error/src/lib.rs +++ b/vendor/quick-error/src/lib.rs @@ -45,7 +45,7 @@ //! Now you might have noticed trailing braces `{}`. They are used to define //! implementations. By default: //! -//! * `Error::source()` returns None (even if type wraps some value) +//! * `Error::cause()` returns None (even if type wraps some value) //! * `Display` outputs debug representation //! * No `From` implementations are defined //! @@ -66,7 +66,7 @@ //! } //! ``` //! -//! To change `source` method to return some error, add `source(value)`, for +//! To change `cause` method to return some error, add `cause(value)`, for //! example: //! //! ```rust @@ -77,19 +77,19 @@ //! #[derive(Debug)] //! pub enum SomeError { //! Io(err: std::io::Error) { -//! source(err) +//! cause(err) //! } //! Utf8(err: std::str::Utf8Error) { //! display("utf8 error") //! } //! Other(err: Box<std::error::Error>) { -//! source(&**err) +//! cause(&**err) //! } //! } //! } //! ``` //! Note you don't need to wrap value in `Some`, its implicit. In case you want -//! `None` returned just omit the `source`. You can't return `None` +//! `None` returned just omit the `cause`. You can't return `None` //! conditionally. //! //! To change how each clause is `Display`ed add `display(pattern,..args)`, @@ -220,12 +220,12 @@ //! } //! //! fn openfile(path: &Path) -> Result<(), Error> { -//! File::open(path).context(path)?; +//! try!(File::open(path).context(path)); //! //! // If we didn't have context, the line above would be written as; //! // -//! // File::open(path) -//! // .map_err(|err| Error::File(path.to_path_buf(), err))?; +//! // try!(File::open(path) +//! // .map_err(|err| Error::File(path.to_path_buf(), err))); //! //! Ok(()) //! } @@ -248,7 +248,7 @@ //! //! More info on context in [this article](http://bit.ly/1PsuxDt). //! -//! All forms of `from`, `display`, `source`, and `context` +//! All forms of `from`, `display`, `cause`, and `context` //! clauses can be combined and put in arbitrary order. Only `from` and //! `context` can be used multiple times in single variant of enumeration. //! Docstrings are also okay. Empty braces can be omitted as of quick_error @@ -293,6 +293,7 @@ //! //! + /// Main macro that does all the work #[macro_export] macro_rules! quick_error { @@ -370,8 +371,9 @@ macro_rules! quick_error { } impl ::std::error::Error for $strname { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - self.0.source() + #[allow(deprecated)] + fn cause(&self) -> Option<&::std::error::Error> { + self.0.cause() } } }; @@ -452,6 +454,8 @@ macro_rules! quick_error { queue [ #[$qmeta:meta] $( $tail:tt )*] ) => { quick_error!(SORT [$( $def )*] + enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )* + $(#[$bmeta])* => $bitem: $bmode $(( $($btyp),* ))*] items [$($( #[$imeta:meta] )* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )* $bitem: $bmode [$( $bvar:$btyp ),*] {} ] @@ -468,7 +472,7 @@ macro_rules! quick_error { ) => { quick_error!(SORT [$( $def )*] items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] - buf [$( #[$bmeta] )* => $bitem: TUPLE [$( $qvar:$qtyp ),+] ] + buf [$( #[$bmeta] )* => $bitem: TUPLE [$( $qvar:$qtyp ),*] ] queue [$( $tail )*] ); }; @@ -482,7 +486,7 @@ macro_rules! quick_error { ) => { quick_error!(SORT [$( $def )*] items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] - buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),+] ] + buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ] queue [$( $tail )*]); }; // Add struct enum-variant, with excess comma - e.g. { descr: &'static str, } @@ -495,7 +499,7 @@ macro_rules! quick_error { ) => { quick_error!(SORT [$( $def )*] items [$( $(#[$imeta])* => $iitem: $imode [$( $ivar:$ityp ),*] {$( $ifuncs )*} )*] - buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),+] ] + buf [$( #[$bmeta] )* => $bitem: STRUCT [$( $qvar:$qtyp ),*] ] queue [$( $tail )*]); }; // Add braces and flush always on braces @@ -557,7 +561,7 @@ macro_rules! quick_error { pub enum $name { $( $(#[$imeta])* - $iitem $(($( $ttyp ),+))* $({$( $svar: $styp ),*})*, + $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*, )* } }; @@ -575,7 +579,7 @@ macro_rules! quick_error { enum $name { $( $(#[$imeta])* - $iitem $(($( $ttyp ),+))* $({$( $svar: $styp ),*})*, + $iitem $(($( $ttyp ),*))* $({$( $svar: $styp ),*})*, )* } }; @@ -601,7 +605,7 @@ macro_rules! quick_error { ) => { quick_error!(ENUM_DEFINITION [ $($def)* ] body [$($( #[$imeta] )* => $iitem ($(($( $ttyp ),+))*) {$({$( $svar: $styp ),*})*} )* - $( #[$qmeta] )* => $qitem (($( $qtyp ),+)) {} ] + $( #[$qmeta] )* => $qitem (($( $qtyp ),*)) {} ] queue [ $($queue)* ] ); }; @@ -654,14 +658,14 @@ macro_rules! quick_error { #[allow(unused_doc_comment)] #[allow(unused_doc_comments)] impl ::std::error::Error for $name { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + fn cause(&self) -> Option<&::std::error::Error> { match *self { $( $(#[$imeta])* quick_error!(ITEM_PATTERN $name $item: $imode [$( ref $var ),*] ) => { - quick_error!(FIND_SOURCE_IMPL + quick_error!(FIND_CAUSE_IMPL $item: $imode [$( $var ),*] {$( $funcs )*}) } @@ -709,21 +713,33 @@ macro_rules! quick_error { write!(f, "{:?}", self_) } }; - (FIND_SOURCE_IMPL $item:ident: $imode:tt + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { description($expr:expr) $( $tail:tt )*} + ) => {}; + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { $t:tt $( $tail:tt )*} + ) => {}; + (FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident + [$( $var:ident ),*] + { } + ) => {}; + (FIND_CAUSE_IMPL $item:ident: $imode:tt [$( $var:ident ),*] - { source($expr:expr) $( $tail:tt )*} + { cause($expr:expr) $( $tail:tt )*} ) => { Some($expr) }; - (FIND_SOURCE_IMPL $item:ident: $imode:tt + (FIND_CAUSE_IMPL $item:ident: $imode:tt [$( $var:ident ),*] { $t:tt $( $tail:tt )*} ) => { - quick_error!(FIND_SOURCE_IMPL + quick_error!(FIND_CAUSE_IMPL $item: $imode [$( $var ),*] { $($tail)* }) }; - (FIND_SOURCE_IMPL $item:ident: $imode:tt + (FIND_CAUSE_IMPL $item:ident: $imode:tt [$( $var:ident ),*] { } ) => { @@ -924,7 +940,9 @@ macro_rules! quick_error { => { quick_error!(ERROR_CHECK $imode $($tail)*); }; (ERROR_CHECK $imode:tt display($pattern: expr, $( $exprs:tt )*) $( $tail:tt )*) => { quick_error!(ERROR_CHECK $imode $($tail)*); }; - (ERROR_CHECK $imode:tt source($expr:expr) $($tail:tt)*) + (ERROR_CHECK $imode:tt description($expr:expr) $( $tail:tt )*) + => { quick_error!(ERROR_CHECK $imode $($tail)*); }; + (ERROR_CHECK $imode:tt cause($expr:expr) $($tail:tt)*) => { quick_error!(ERROR_CHECK $imode $($tail)*); }; (ERROR_CHECK $imode:tt from() $($tail:tt)*) => { quick_error!(ERROR_CHECK $imode $($tail)*); }; @@ -947,6 +965,7 @@ macro_rules! quick_error { (IDENT $ident:ident) => { $ident } } + /// Generic context type /// /// Used mostly as a transport for `ResultExt::context` method @@ -970,13 +989,16 @@ impl<T, E> ResultExt<T, E> for Result<T, E> { } } + + #[cfg(test)] +#[allow(deprecated)] mod test { - use std::error::Error; use std::num::{ParseFloatError, ParseIntError}; - use std::path::{Path, PathBuf}; use std::str::Utf8Error; use std::string::FromUtf8Error; + use std::error::Error; + use std::path::{Path, PathBuf}; use super::ResultExt; @@ -992,15 +1014,14 @@ mod test { fn bare_item_direct() { assert_eq!(format!("{}", Bare::One), "One".to_string()); assert_eq!(format!("{:?}", Bare::One), "One".to_string()); - assert!(Bare::One.source().is_none()); + assert!(Bare::One.cause().is_none()); } - #[test] fn bare_item_trait() { - let err: &dyn Error = &Bare::Two; + let err: &Error = &Bare::Two; assert_eq!(format!("{}", err), "Two".to_string()); assert_eq!(format!("{:?}", err), "Two".to_string()); - assert!(err.source().is_none()); + assert!(err.cause().is_none()); } quick_error! { @@ -1016,18 +1037,13 @@ mod test { #[test] fn wrapper() { - assert_eq!( - format!("{}", Wrapper::from(Wrapped::One)), - "One".to_string() - ); - assert_eq!( - format!("{}", Wrapper::from(Wrapped::from(String::from("hello")))), - "two: hello".to_string() - ); - assert_eq!( - format!("{:?}", Wrapper::from(Wrapped::One)), - "Wrapper(One)".to_string() - ); + assert_eq!(format!("{}", Wrapper::from(Wrapped::One)), + "One".to_string()); + assert_eq!(format!("{}", + Wrapper::from(Wrapped::from(String::from("hello")))), + "two: hello".to_string()); + assert_eq!(format!("{:?}", Wrapper::from(Wrapped::One)), + "Wrapper(One)".to_string()); } quick_error! { @@ -1037,14 +1053,14 @@ mod test { ParseFloatError(err: ParseFloatError) { from() display("parse float error: {err}", err=err) - source(err) + cause(err) } Other(descr: &'static str) { display("Error: {}", descr) } /// FromUtf8 Error FromUtf8Error(err: Utf8Error, source: Vec<u8>) { - source(err) + cause(err) display(me) -> ("{desc} at index {pos}: {err}", desc="utf8 error", pos=err.valid_up_to(), err=err) from(err: FromUtf8Error) -> (err.utf8_error().clone(), err.into_bytes()) } @@ -1059,68 +1075,46 @@ mod test { #[test] fn tuple_wrapper_err() { - let source = "one and a half times pi".parse::<f32>().unwrap_err(); - let err = TupleWrapper::ParseFloatError(source.clone()); - assert_eq!(format!("{}", err), format!("parse float error: {}", source)); - assert_eq!( - format!("{:?}", err), - format!("ParseFloatError({:?})", source) - ); - assert_eq!( - format!("{:?}", err.source().unwrap()), - format!("{:?}", source) - ); + let cause = "one and a half times pi".parse::<f32>().unwrap_err(); + let err = TupleWrapper::ParseFloatError(cause.clone()); + assert_eq!(format!("{}", err), format!("parse float error: {}", cause)); + assert_eq!(format!("{:?}", err), format!("ParseFloatError({:?})", cause)); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); } #[test] fn tuple_wrapper_trait_str() { let desc = "hello"; - let err: &dyn Error = &TupleWrapper::Other(desc); + let err: &Error = &TupleWrapper::Other(desc); assert_eq!(format!("{}", err), format!("Error: {}", desc)); assert_eq!(format!("{:?}", err), format!("Other({:?})", desc)); - assert!(err.source().is_none()); + assert!(err.cause().is_none()); } #[test] fn tuple_wrapper_trait_two_fields() { let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; - let source = String::from_utf8(invalid_utf8.clone()) - .unwrap_err() - .utf8_error(); - let err: &dyn Error = &TupleWrapper::FromUtf8Error(source.clone(), invalid_utf8.clone()); - assert_eq!( - format!("{}", err), - format!( - "{desc} at index {pos}: {source}", - desc = "utf8 error", - pos = source.valid_up_to(), - source = source - ) - ); - assert_eq!( - format!("{:?}", err), - format!("FromUtf8Error({:?}, {:?})", source, invalid_utf8) - ); - assert_eq!( - format!("{:?}", err.source().unwrap()), - format!("{:?}", source) - ); + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err: &Error = &TupleWrapper::FromUtf8Error(cause.clone(), invalid_utf8.clone()); + assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc="utf8 error", pos=cause.valid_up_to(), cause=cause)); + assert_eq!(format!("{:?}", err), format!("FromUtf8Error({:?}, {:?})", cause, invalid_utf8)); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); } #[test] fn tuple_wrapper_from() { - let source = "one and a half times pi".parse::<f32>().unwrap_err(); - let err = TupleWrapper::ParseFloatError(source.clone()); - let err_from: TupleWrapper = From::from(source); + let cause = "one and a half times pi".parse::<f32>().unwrap_err(); + let err = TupleWrapper::ParseFloatError(cause.clone()); + let err_from: TupleWrapper = From::from(cause); assert_eq!(err_from, err); } #[test] fn tuple_wrapper_custom_from() { let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; - let source = String::from_utf8(invalid_utf8.clone()).unwrap_err(); - let err = TupleWrapper::FromUtf8Error(source.utf8_error().clone(), invalid_utf8); - let err_from: TupleWrapper = From::from(source); + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err(); + let err = TupleWrapper::FromUtf8Error(cause.utf8_error().clone(), invalid_utf8); + let err_from: TupleWrapper = From::from(cause); assert_eq!(err_from, err); } @@ -1129,7 +1123,7 @@ mod test { let err: TupleWrapper = From::from("hello"); assert_eq!(format!("{}", err), format!("Discard")); assert_eq!(format!("{:?}", err), format!("Discard")); - assert!(err.source().is_none()); + assert!(err.cause().is_none()); } #[test] @@ -1137,7 +1131,7 @@ mod test { let err: TupleWrapper = TupleWrapper::Singleton; assert_eq!(format!("{}", err), format!("Just a string")); assert_eq!(format!("{:?}", err), format!("Singleton")); - assert!(err.source().is_none()); + assert!(err.cause().is_none()); } quick_error! { @@ -1145,7 +1139,7 @@ mod test { pub enum StructWrapper { // Utf8 Error Utf8Error{ err: Utf8Error, hint: Option<&'static str> } { - source(err) + cause(err) display(me) -> ("{desc} at index {pos}: {err}", desc="utf8 error", pos=err.valid_up_to(), err=err) from(err: Utf8Error) -> { err: err, hint: None } } @@ -1159,47 +1153,19 @@ mod test { #[test] fn struct_wrapper_err() { let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; - let source = String::from_utf8(invalid_utf8.clone()) - .unwrap_err() - .utf8_error(); - let err: &dyn Error = &StructWrapper::Utf8Error { - err: source.clone(), - hint: Some("nonsense"), - }; - assert_eq!( - format!("{}", err), - format!( - "{desc} at index {pos}: {source}", - desc = "utf8 error", - pos = source.valid_up_to(), - source = source - ) - ); - assert_eq!( - format!("{:?}", err), - format!( - "Utf8Error {{ err: {:?}, hint: {:?} }}", - source, - Some("nonsense") - ) - ); - assert_eq!( - format!("{:?}", err.source().unwrap()), - format!("{:?}", source) - ); + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err: &Error = &StructWrapper::Utf8Error{ err: cause.clone(), hint: Some("nonsense") }; + assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc="utf8 error", pos=cause.valid_up_to(), cause=cause)); + assert_eq!(format!("{:?}", err), format!("Utf8Error {{ err: {:?}, hint: {:?} }}", cause, Some("nonsense"))); + assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause)); } #[test] fn struct_wrapper_struct_from() { let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; - let source = String::from_utf8(invalid_utf8.clone()) - .unwrap_err() - .utf8_error(); - let err = StructWrapper::Utf8Error { - err: source.clone(), - hint: None, - }; - let err_from: StructWrapper = From::from(source); + let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error(); + let err = StructWrapper::Utf8Error{ err: cause.clone(), hint: None }; + let err_from: StructWrapper = From::from(cause); assert_eq!(err_from, err); } @@ -1208,11 +1174,8 @@ mod test { let descr = "hello"; let err = StructWrapper::ExcessComma { descr: descr }; assert_eq!(format!("{}", err), format!("Error: {}", descr)); - assert_eq!( - format!("{:?}", err), - format!("ExcessComma {{ descr: {:?} }}", descr) - ); - assert!(err.source().is_none()); + assert_eq!(format!("{:?}", err), format!("ExcessComma {{ descr: {:?} }}", descr)); + assert!(err.cause().is_none()); } quick_error! { @@ -1243,23 +1206,19 @@ mod test { #[test] fn parse_float_error() { fn parse_float(s: &str) -> Result<f32, ContextErr> { - Ok(s.parse().context(s)?) + Ok(try!(s.parse().context(s))) } - assert_eq!( - format!("{}", parse_float("12ab").unwrap_err()), - r#"Float error "12ab": invalid float literal"# - ); + assert_eq!(format!("{}", parse_float("12ab").unwrap_err()), + r#"Float error "12ab": invalid float literal"#); } #[test] fn parse_int_error() { fn parse_int(s: &str) -> Result<i32, ContextErr> { - Ok(s.parse().context(s)?) + Ok(try!(s.parse().context(s))) } - assert_eq!( - format!("{}", parse_int("12.5").unwrap_err()), - r#"Int error "12.5": invalid digit found in string"# - ); + assert_eq!(format!("{}", parse_int("12.5").unwrap_err()), + r#"Int error "12.5": invalid digit found in string"#); } #[test] @@ -1268,24 +1227,25 @@ mod test { s.parse().context(s).unwrap() } assert_eq!(parse_int("12"), 12); - assert_eq!( - format!("{:?}", "x".parse::<i32>().context("x")), - r#"Err(Context("x", ParseIntError { kind: InvalidDigit }))"# - ); + assert_eq!(format!("{:?}", "x".parse::<i32>().context("x")), + r#"Err(Context("x", ParseIntError { kind: InvalidDigit }))"#); } #[test] fn path_context() { - fn parse_utf<P: AsRef<Path>>(s: &[u8], p: P) -> Result<(), ContextErr> { - ::std::str::from_utf8(s).context(p)?; + fn parse_utf<P: AsRef<Path>>(s: &[u8], p: P) + -> Result<(), ContextErr> + { + try!(::std::str::from_utf8(s).context(p)); Ok(()) } let etext = parse_utf(b"a\x80\x80", "/etc").unwrap_err().to_string(); - assert!(etext.starts_with("Path error at \"/etc\": invalid utf-8")); - let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp")) - .unwrap_err() + assert!(etext.starts_with( + "Path error at \"/etc\": invalid utf-8")); + let etext = parse_utf(b"\x80\x80", PathBuf::from("/tmp")).unwrap_err() .to_string(); - assert!(etext.starts_with("Path error at \"/tmp\": invalid utf-8")); + assert!(etext.starts_with( + "Path error at \"/tmp\": invalid utf-8")); } #[test] @@ -1299,38 +1259,4 @@ mod test { } } } - - #[test] - #[allow(deprecated)] - fn cause_struct_wrapper_err() { - let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150]; - let cause = String::from_utf8(invalid_utf8.clone()) - .unwrap_err() - .utf8_error(); - let err: &dyn Error = &StructWrapper::Utf8Error { - err: cause.clone(), - hint: Some("nonsense"), - }; - assert_eq!( - format!("{}", err), - format!( - "{desc} at index {pos}: {cause}", - desc = "utf8 error", - pos = cause.valid_up_to(), - cause = cause - ) - ); - assert_eq!( - format!("{:?}", err), - format!( - "Utf8Error {{ err: {:?}, hint: {:?} }}", - cause, - Some("nonsense") - ) - ); - assert_eq!( - format!("{:?}", err.cause().unwrap()), - format!("{:?}", cause) - ); - } } |