oct/error/
usize_encode_error.rs

1// Copyright 2024-2025 Gabriel Bjørnager Jensen.
2//
3// This Source Code Form is subject to the terms of
4// the Mozilla Public License, v. 2.0. If a copy of
5// the MPL was not distributed with this file, you
6// can obtain one at:
7// <https://mozilla.org/MPL/2.0/>.
8
9use core::convert::Infallible;
10use core::error::Error;
11use core::fmt::{self, Display, Formatter};
12
13/// A [`usize`] value could not be decoded.
14///
15/// Any `usize` object that can fit in an [`u16`] can be encoded successfully.
16#[derive(Debug, Eq, PartialEq)]
17#[must_use]
18pub struct UsizeEncodeError(
19	/// The unencodable value.
20	pub usize,
21);
22
23impl Display for UsizeEncodeError {
24	#[inline]
25	fn fmt(&self, f: &mut Formatter) -> fmt::Result {
26		write!(
27			f,
28			"unsigned size value `{}` cannot be serialised: must be at most `{}`",
29			self.0,
30			u16::MAX,
31		)
32	}
33}
34
35impl Error for UsizeEncodeError { }
36
37impl From<Infallible> for UsizeEncodeError {
38	#[inline(always)]
39	fn from(_value: Infallible) -> Self {
40		unreachable!()
41	}
42}