#[non_exhaustive]pub struct BitError {
pub kind: ErrorKind,
pub at: usize,
pub field: Option<&'static str>,
}Expand description
A position-aware bit-codec error (it carries a span-like position). It records the bit offset where decoding/encoding failed and, when the derive can supply it, the field being processed.
§Examples
use bnb::{bin, ErrorKind};
#[bin(big)]
#[derive(Debug)]
struct Pair { a: u16, b: u16 }
let err = Pair::decode_exact(&[0x00]).unwrap_err(); // only one byte of four
assert_eq!(err.at, 0); // the bit offset where it failed
assert_eq!(err.field, Some("a")); // the field being read (the span)
assert!(matches!(err.kind, ErrorKind::UnexpectedEof { .. }));Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.kind: ErrorKindThe cause.
at: usizeAbsolute bit offset where the error occurred.
field: Option<&'static str>The field being decoded/encoded when it occurred, if recorded by the
derive (the innermost field — the “span”). None for low-level reader
errors with no field context.
Implementations§
Source§impl BitError
impl BitError
Sourcepub fn new(kind: ErrorKind, at: usize) -> Self
pub fn new(kind: ErrorKind, at: usize) -> Self
Builds an error at absolute bit offset at, with no field recorded yet.
Sourcepub fn bad_magic(expected: u128, found: u128, at: usize) -> Self
pub fn bad_magic(expected: u128, found: u128, at: usize) -> Self
Builds a ErrorKind::BadMagic error (a magic constant mismatched) at
absolute bit offset at. expected/found are the type-erased low-bit
values (Bits::into_bits).
Sourcepub fn convert(message: String, at: usize) -> Self
pub fn convert(message: String, at: usize) -> Self
Builds a ErrorKind::Convert error (a try_map conversion failed) at
absolute bit offset at.
Sourcepub fn in_field(self, field: &'static str) -> Self
pub fn in_field(self, field: &'static str) -> Self
Records the field being processed, if one is not already set — so the innermost field (set first as the error propagates up) wins. The derive calls this per field.
Sourcepub fn is_incomplete(&self) -> bool
pub fn is_incomplete(&self) -> bool
Whether this is the streaming “need more bytes” signal
(ErrorKind::Incomplete) — the caller should read more and retry, as
opposed to a definitive parse failure.
Trait Implementations§
impl Eq for BitError
Source§impl Error for BitError
impl Error for BitError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<Error> for BitError
Available on crate feature std only.
impl From<Error> for BitError
std only.Source§fn from(e: Error) -> Self
fn from(e: Error) -> Self
Wraps a std::io::Error as ErrorKind::Io — so a parse_with/write_with
using Source::as_read/Sink::as_write can ? std::io results straight
into a BitError. The bit offset is unknown at this boundary (recorded as 0);
build with BitError::new if you need the precise position.
Source§impl From<WidthError> for BitError
impl From<WidthError> for BitError
Source§fn from(e: WidthError) -> Self
fn from(e: WidthError) -> Self
Bridges a construction error (e.g. UInt::try_new) into a codec error, so it
?-propagates inside a custom parse_with/write_with fn or a converter
that returns BitError. The offset is unknown (0) — the codec’s own
reads/writes carry the real bit offset; this is only for borrowed construction
failures with no cursor context.