Skip to content

Commit f42ccc1

Browse files
committed
Add new feature flags
1 parent 0270436 commit f42ccc1

13 files changed

+197
-9
lines changed

Cargo.toml

+16-2
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,19 @@ categories = [
2525
readme = "README.md"
2626
rust-version = "1.70"
2727

28-
[dependencies]
29-
traitful = "0.3.0"
28+
[package.metadata.docs.rs]
29+
all-features = true
30+
31+
[dependencies.traitful]
32+
version = "0.3.0"
33+
34+
[features]
35+
# Enable the `io` module, MSRV 1.84 for `Pin::as_deref_mut()`
36+
#
37+
# Unstable API has no stability guarantees
38+
unstable-io = []
39+
40+
# Enable usage of the `Error` trait in core, MSRV 1.81
41+
#
42+
# Unstable API has no stability guarantees
43+
unstable-error = []

src/error/end.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
use core::num::NonZeroUsize;
1+
use core::{fmt, num::NonZeroUsize};
22

33
/// Expected buffer to end, but it didn't
44
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
55
#[non_exhaustive]
66
pub struct EndError(Option<NonZeroUsize>);
77

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for EndError {}
11+
12+
impl fmt::Display for EndError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("expected buffer to end, but it didn't")?;
15+
16+
if let Some(remaining) = self.0 {
17+
write!(f, ", {remaining} bytes remaining")?;
18+
}
19+
20+
Ok(())
21+
}
22+
}
23+
824
impl EndError {
925
/// Create a new [`EndError`].
1026
pub const fn new() -> Self {

src/error/flush.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::fmt;
2+
13
use crate::error::{FullError, LostError};
24

35
/// Flush error
@@ -9,6 +11,21 @@ pub enum FlushError {
911
Lost(LostError),
1012
}
1113

14+
/// __*`unstable-error`*__: feature required
15+
#[cfg(feature = "unstable-error")]
16+
impl core::error::Error for FlushError {}
17+
18+
impl fmt::Display for FlushError {
19+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20+
f.write_str("flush error: ")?;
21+
22+
match self {
23+
Self::Full(err) => fmt::Display::fmt(err, f),
24+
Self::Lost(err) => fmt::Display::fmt(err, f),
25+
}
26+
}
27+
}
28+
1229
impl From<FullError> for FlushError {
1330
fn from(error: FullError) -> Self {
1431
Self::Full(error)

src/error/full.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
use core::num::NonZeroUsize;
1+
use core::{fmt, num::NonZeroUsize};
22

33
/// Destination has run out of space
44
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
55
#[non_exhaustive]
66
pub struct FullError(Option<NonZeroUsize>);
77

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for FullError {}
11+
12+
impl fmt::Display for FullError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("destination has run out of space")?;
15+
16+
if let Some(remaining) = self.0 {
17+
write!(f, ", {remaining} bytes remaining")?;
18+
}
19+
20+
Ok(())
21+
}
22+
}
23+
824
impl FullError {
925
/// Create a new [`FullError`].
1026
pub const fn new() -> Self {

src/error/len.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
use core::num::NonZeroUsize;
1+
use core::{fmt, num::NonZeroUsize};
22

33
/// Source ran over the end of the buffer
44
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
55
#[non_exhaustive]
66
pub struct LenError(Option<NonZeroUsize>);
77

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for LenError {}
11+
12+
impl fmt::Display for LenError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("source ran over the end of the buffer")?;
15+
16+
if let Some(remaining) = self.0 {
17+
write!(f, ", {remaining} bytes remaining")?;
18+
}
19+
20+
Ok(())
21+
}
22+
}
23+
824
impl LenError {
925
/// Create a new [`LenError`].
1026
pub const fn new() -> Self {

src/error/lost.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
use core::fmt;
2+
13
/// Destination lost (from either corruption or disconnection)
24
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
35
#[non_exhaustive]
46
pub struct LostError(Option<&'static str>);
57

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for LostError {}
11+
12+
impl fmt::Display for LostError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("destination lost")?;
15+
16+
if let Some(message) = self.0 {
17+
f.write_str(", ")?;
18+
f.write_str(message)?;
19+
}
20+
21+
Ok(())
22+
}
23+
}
24+
625
impl LostError {
726
/// Create a new [`LostError`].
827
pub fn new() -> Self {

src/error/overflow.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
/// Overflow (variable can't contain parsed value)
1+
use core::fmt;
2+
3+
/// Value overflow (variable can't contain parsed value)
24
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
35
#[non_exhaustive]
46
pub struct OverflowError();
57

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for OverflowError {}
11+
12+
impl fmt::Display for OverflowError {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("value overflow")
15+
}
16+
}
17+
618
impl OverflowError {
719
/// Create a new [`OverflowError`].
8-
pub fn new() -> Self {
20+
pub const fn new() -> Self {
921
Self()
1022
}
1123
}

src/error/parsing.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::fmt;
2+
13
use crate::error::{
24
EndError, FlushError, FullError, LenError, LostError, OverflowError,
35
StrError, Uleb128Error, Utf8Error,
@@ -21,6 +23,25 @@ pub enum Error {
2123
Lost(LostError),
2224
}
2325

26+
/// __*`unstable-error`*__: feature required
27+
#[cfg(feature = "unstable-error")]
28+
impl core::error::Error for Error {}
29+
30+
impl fmt::Display for Error {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
f.write_str("parsing error: ")?;
33+
34+
match self {
35+
Self::Len(err) => fmt::Display::fmt(err, f),
36+
Self::End(err) => fmt::Display::fmt(err, f),
37+
Self::Utf8(err) => fmt::Display::fmt(err, f),
38+
Self::Overflow(err) => fmt::Display::fmt(err, f),
39+
Self::Full(err) => fmt::Display::fmt(err, f),
40+
Self::Lost(err) => fmt::Display::fmt(err, f),
41+
}
42+
}
43+
}
44+
2445
impl From<LenError> for Error {
2546
fn from(error: LenError) -> Self {
2647
Self::Len(error)

src/error/str.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::fmt;
2+
13
use crate::error::{LenError, Utf8Error};
24

35
/// String parsing error
@@ -9,6 +11,21 @@ pub enum StrError {
911
Utf8(Utf8Error),
1012
}
1113

14+
/// __*`unstable-error`*__: feature required
15+
#[cfg(feature = "unstable-error")]
16+
impl core::error::Error for StrError {}
17+
18+
impl fmt::Display for StrError {
19+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20+
f.write_str("string parsing error: ")?;
21+
22+
match self {
23+
Self::Len(err) => fmt::Display::fmt(err, f),
24+
Self::Utf8(err) => fmt::Display::fmt(err, f),
25+
}
26+
}
27+
}
28+
1229
impl From<LenError> for StrError {
1330
fn from(error: LenError) -> Self {
1431
Self::Len(error)

src/error/uleb128.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::fmt;
2+
13
use crate::error::{LenError, OverflowError};
24

35
/// ULEB128 parsing error
@@ -9,6 +11,21 @@ pub enum Uleb128Error {
911
Overflow(OverflowError),
1012
}
1113

14+
/// __*`unstable-error`*__: feature required
15+
#[cfg(feature = "unstable-error")]
16+
impl core::error::Error for Uleb128Error {}
17+
18+
impl fmt::Display for Uleb128Error {
19+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20+
f.write_str("uleb128 parsing error: ")?;
21+
22+
match self {
23+
Self::Len(err) => fmt::Display::fmt(err, f),
24+
Self::Overflow(err) => fmt::Display::fmt(err, f),
25+
}
26+
}
27+
}
28+
1229
impl From<LenError> for Uleb128Error {
1330
fn from(error: LenError) -> Self {
1431
Self::Len(error)

src/error/utf8.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
use core::str;
1+
use core::{fmt, str};
22

33
/// Invalid UTF-8
44
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
55
#[non_exhaustive]
66
pub struct Utf8Error(str::Utf8Error);
77

8+
/// __*`unstable-error`*__: feature required
9+
#[cfg(feature = "unstable-error")]
10+
impl core::error::Error for Utf8Error {}
11+
12+
impl fmt::Display for Utf8Error {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
f.write_str("invalid utf8: ")?;
15+
fmt::Display::fmt(&self.0, f)
16+
}
17+
}
18+
819
impl Utf8Error {
920
/// Return the offset from the given reader's cursor up to which valid UTF-8
1021
/// was verified.

src/io/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! I/O primitives
1+
//! __*`unstable-io`*__ feature required; I/O primitives (MSRV 1.84)
22
33
mod destination;
44
mod receiver;

src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
//! bound reading and writing, there are the [`io::Source`] and
1919
//! [`io::Destination`] traits which work by buffering the bytes to be sent on
2020
//! an [`io::Sender`] or received on an [`io::Receiver`].
21+
//!
22+
//! # Features
23+
//!
24+
//! Some non-default features can enable unstable (no API stability guarantees)
25+
//! functionality.
26+
//!
27+
//! - __*`unstable-io`*__: Bumps MSRV to 1.84, enables the [`io`] module
28+
//! - __*`unstable-error`*__: Bumps MSRV to 1.81, implements [`Error`] for
29+
//! error types
30+
//!
31+
//! [`Error`]: core::error::Error
2132
2233
#![doc(
2334
html_logo_url = "https://ardaku.github.io/mm/logo.svg",
@@ -45,6 +56,7 @@ pub mod be;
4556
pub mod class;
4657
mod empty;
4758
pub mod error;
59+
#[cfg(feature = "unstable-io")]
4860
pub mod io;
4961
pub mod le;
5062
mod purge;

0 commit comments

Comments
 (0)