pub struct CodecError { /* private fields */ }Expand description
Error returned by canonical encoding and decoding operations.
Implementations§
Source§impl CodecError
impl CodecError
Sourcepub const fn new(kind: CodecErrorKind, message: &'static str) -> Self
pub const fn new(kind: CodecErrorKind, message: &'static str) -> Self
Creates a new codec error with a stable kind and actionable message.
Sourcepub const fn kind(&self) -> CodecErrorKind
pub const fn kind(&self) -> CodecErrorKind
Returns the stable error category.
Examples found in repository?
examples/protocol_message.rs (line 72)
58fn main() -> Result<(), CodecError> {
59 let message = ProtocolMessage {
60 id: 42,
61 kind: MessageKind::Data(vec![1, 2, 3]),
62 };
63
64 let encoded = encode_to_vec(&message)?;
65 assert_eq!(encoded, [42, 0, 0, 0, 1, 3, 0, 0, 0, 1, 2, 3]);
66 assert_eq!(
67 decode_from_slice_exact::<ProtocolMessage>(&encoded)?,
68 message
69 );
70
71 let err = decode_from_slice_exact::<MessageKind>(&[9]).unwrap_err();
72 assert_eq!(err.kind(), CodecErrorKind::InvalidValue);
73 assert!(err.message().contains("unknown MessageKind tag"));
74
75 Ok(())
76}More examples
examples/basic_encoding.rs (line 21)
5fn main() -> Result<(), CodecError> {
6 let port = 8080u16;
7 let encoded = encode_to_vec(&port)?;
8 assert_eq!(encoded, [0x90, 0x1f]);
9 assert_eq!(decode_from_slice_exact::<u16>(&encoded)?, port);
10
11 let text = "api";
12 let encoded = encode_to_vec(text)?;
13 assert_eq!(encoded, [3, 0, 0, 0, b'a', b'p', b'i']);
14 assert_eq!(decode_from_slice_exact::<String>(&encoded)?, text);
15
16 let (value, remaining) = decode_from_slice::<u8>(&[1, 2])?;
17 assert_eq!(value, 1);
18 assert_eq!(remaining, 1);
19
20 let err = decode_from_slice_exact::<u8>(&[1, 2]).unwrap_err();
21 assert_eq!(err.kind(), CodecErrorKind::TrailingBytes);
22
23 Ok(())
24}Sourcepub const fn message(&self) -> &'static str
pub const fn message(&self) -> &'static str
Returns a human-readable error message.
Examples found in repository?
examples/protocol_message.rs (line 73)
58fn main() -> Result<(), CodecError> {
59 let message = ProtocolMessage {
60 id: 42,
61 kind: MessageKind::Data(vec![1, 2, 3]),
62 };
63
64 let encoded = encode_to_vec(&message)?;
65 assert_eq!(encoded, [42, 0, 0, 0, 1, 3, 0, 0, 0, 1, 2, 3]);
66 assert_eq!(
67 decode_from_slice_exact::<ProtocolMessage>(&encoded)?,
68 message
69 );
70
71 let err = decode_from_slice_exact::<MessageKind>(&[9]).unwrap_err();
72 assert_eq!(err.kind(), CodecErrorKind::InvalidValue);
73 assert!(err.message().contains("unknown MessageKind tag"));
74
75 Ok(())
76}Sourcepub const fn unexpected_eof() -> Self
pub const fn unexpected_eof() -> Self
Input ended before the requested bytes could be read.
Sourcepub const fn invalid_value(message: &'static str) -> Self
pub const fn invalid_value(message: &'static str) -> Self
Value used an invalid byte or tag.
Sourcepub const fn length_overflow(message: &'static str) -> Self
pub const fn length_overflow(message: &'static str) -> Self
Decoded length cannot be represented or safely processed.
Sourcepub const fn trailing_bytes() -> Self
pub const fn trailing_bytes() -> Self
Exact decode found bytes after the decoded value.
Sourcepub const fn write_failed() -> Self
pub const fn write_failed() -> Self
Writer failed to accept bytes.
Sourcepub const fn read_failed() -> Self
pub const fn read_failed() -> Self
Reader failed for a reason other than end of input.
Trait Implementations§
Source§impl Clone for CodecError
impl Clone for CodecError
Source§fn clone(&self) -> CodecError
fn clone(&self) -> CodecError
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for CodecError
Source§impl Debug for CodecError
impl Debug for CodecError
Source§impl Display for CodecError
impl Display for CodecError
impl Eq for CodecError
Source§impl Error for CodecError
Available on crate feature std only.
impl Error for CodecError
Available on crate feature
std only.1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl PartialEq for CodecError
impl PartialEq for CodecError
Source§fn eq(&self, other: &CodecError) -> bool
fn eq(&self, other: &CodecError) -> bool
Tests for
self and other values to be equal, and is used by ==.impl StructuralPartialEq for CodecError
Auto Trait Implementations§
impl Freeze for CodecError
impl RefUnwindSafe for CodecError
impl Send for CodecError
impl Sync for CodecError
impl Unpin for CodecError
impl UnsafeUnpin for CodecError
impl UnwindSafe for CodecError
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