Struct bitcoin_internals::error::input_string::InputString
source · pub struct InputString(/* private fields */);
Expand description
Conditionally stores the input string in parse errors.
This type stores the input string of a parse function depending on whether alloc
feature is
enabled. When it is enabled, the string is stored inside as String
. When disabled this is a
zero-sized type and attempt to store a string does nothing.
This provides two methods to format the error strings depending on the context.
Implementations§
source§impl InputString
impl InputString
sourcepub fn display_cannot_parse<'a, T>(&'a self, what: &'a T) -> CannotParse<'a, T>
pub fn display_cannot_parse<'a, T>(&'a self, what: &'a T) -> CannotParse<'a, T>
Displays a message saying failed to parse <self> as <what>
.
This is normally used with the write_err!
macro.
§Examples
use core::fmt;
use bitcoin_internals::error::InputString;
use bitcoin_internals::write_err;
/// An example parsing error including the parse error from core.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
input: InputString,
error: core::num::ParseIntError,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Outputs "failed to parse '<input string>' as foo"
write_err!(f, "{}", self.input.display_cannot_parse("foo"); self.error)
}
}
sourcepub fn unknown_variant<T>(&self, what: &T, f: &mut Formatter<'_>) -> Result
pub fn unknown_variant<T>(&self, what: &T, f: &mut Formatter<'_>) -> Result
Formats a message saying <self> is not a known <what>
.
This is normally used in leaf parse errors (with no source) when parsing an enum.
§Examples
use core::fmt;
use bitcoin_internals::error::InputString;
/// An example parsing error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError(InputString);
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Outputs "'<input string>' is not a known foo"
self.0.unknown_variant("foo", f)
}
}
Trait Implementations§
source§impl Clone for InputString
impl Clone for InputString
source§fn clone(&self) -> InputString
fn clone(&self) -> InputString
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for InputString
impl Debug for InputString
source§impl From<&str> for InputString
impl From<&str> for InputString
source§impl From<String> for InputString
Available on crate feature alloc
only.
impl From<String> for InputString
Available on crate feature
alloc
only.source§impl Hash for InputString
impl Hash for InputString
source§impl Ord for InputString
impl Ord for InputString
source§fn cmp(&self, other: &InputString) -> Ordering
fn cmp(&self, other: &InputString) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq for InputString
impl PartialEq for InputString
source§fn eq(&self, other: &InputString) -> bool
fn eq(&self, other: &InputString) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for InputString
impl PartialOrd for InputString
source§fn partial_cmp(&self, other: &InputString) -> Option<Ordering>
fn partial_cmp(&self, other: &InputString) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moreimpl Eq for InputString
impl StructuralPartialEq for InputString
Auto Trait Implementations§
impl Freeze for InputString
impl RefUnwindSafe for InputString
impl Send for InputString
impl Sync for InputString
impl Unpin for InputString
impl UnwindSafe for InputString
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more