From 72bc7d971a6edba0a1b5f9e0566b85d2459578de Mon Sep 17 00:00:00 2001 From: John-John Tedro Date: Sun, 8 Sep 2024 07:36:45 +0200 Subject: [PATCH] Switch to core::error::Error --- .github/workflows/ci.yml | 2 +- crates/musli-core/Cargo.toml | 2 +- crates/musli-core/src/context.rs | 8 ++++---- crates/musli-core/src/internal/fixed.rs | 3 +-- crates/musli-core/src/no_std.rs | 22 -------------------- crates/musli-macros/Cargo.toml | 2 +- crates/musli-zerocopy-macros/Cargo.toml | 2 +- crates/musli-zerocopy/Cargo.toml | 2 +- crates/musli-zerocopy/src/error.rs | 5 ++--- crates/musli/Cargo.toml | 2 +- crates/musli/src/context/capture.rs | 4 ++-- crates/musli/src/context/context_error.rs | 13 ++++++------ crates/musli/src/context/error_marker.rs | 3 +-- crates/musli/src/context/same.rs | 4 ++-- crates/musli/src/descriptive/error.rs | 11 +++++----- crates/musli/src/json/error.rs | 6 ++---- crates/musli/src/lib.rs | 2 +- crates/musli/src/no_std.rs | 11 +--------- crates/musli/src/reader.rs | 3 +-- crates/musli/src/serde/mod.rs | 6 +++--- crates/musli/src/storage/error.rs | 3 +-- crates/musli/src/str.rs | 25 ++++++++++++++++------- crates/musli/src/value/error.rs | 3 +-- crates/musli/src/wire/error.rs | 3 +-- crates/musli/src/wire/mod.rs | 2 +- crates/musli/tests/default_value.rs | 2 +- crates/musli/tests/untagged_enums.rs | 2 +- tests-macros/Cargo.toml | 2 +- 28 files changed, 62 insertions(+), 93 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b6c1ff571..fc6e538b1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: strategy: fail-fast: false matrix: - rust: ['1.79', stable] + rust: ['1.81', stable] steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master diff --git a/crates/musli-core/Cargo.toml b/crates/musli-core/Cargo.toml index e2b5bd6e7..89b16bda2 100644 --- a/crates/musli-core/Cargo.toml +++ b/crates/musli-core/Cargo.toml @@ -3,7 +3,7 @@ name = "musli-core" version = "0.0.123" authors = ["John-John Tedro "] edition = "2021" -rust-version = "1.79" +rust-version = "1.81" description = """ Core traits and types for the Müsli serialization framework. """ diff --git a/crates/musli-core/src/context.rs b/crates/musli-core/src/context.rs index 82352c429..979501dc3 100644 --- a/crates/musli-core/src/context.rs +++ b/crates/musli-core/src/context.rs @@ -1,11 +1,11 @@ //! Things related to working with contexts. +use core::error::Error; use core::fmt; use core::str; use crate::alloc::Allocator; use crate::de::{DecodeBytes, DecodeUnsized, DecodeUnsizedBytes}; -use crate::no_std; use crate::{Decode, Decoder}; /// Provides ergonomic access to the serialization context. @@ -83,7 +83,7 @@ pub trait Context { #[inline] fn map(&self) -> impl FnOnce(T) -> Self::Error + '_ where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + Error, { move |error| self.custom(error) } @@ -93,7 +93,7 @@ pub trait Context { /// reporting error-like things out from the context. fn custom(&self, error: T) -> Self::Error where - T: 'static + Send + Sync + no_std::Error; + T: 'static + Send + Sync + Error; /// Generate a map function which maps an error using the `message` function. #[inline] @@ -131,7 +131,7 @@ pub trait Context { #[inline(always)] fn marked_custom(&self, mark: Self::Mark, message: T) -> Self::Error where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + Error, { self.custom(message) } diff --git a/crates/musli-core/src/internal/fixed.rs b/crates/musli-core/src/internal/fixed.rs index eadb7861c..1f54c0376 100644 --- a/crates/musli-core/src/internal/fixed.rs +++ b/crates/musli-core/src/internal/fixed.rs @@ -15,8 +15,7 @@ impl fmt::Display for CapacityError { } } -#[cfg(feature = "std")] -impl std::error::Error for CapacityError {} +impl core::error::Error for CapacityError {} /// A fixed capacity vector allocated on the stack. pub(crate) struct FixedVec { diff --git a/crates/musli-core/src/no_std.rs b/crates/musli-core/src/no_std.rs index ca0b475d3..6f8416ee8 100644 --- a/crates/musli-core/src/no_std.rs +++ b/crates/musli-core/src/no_std.rs @@ -2,18 +2,6 @@ //! //! * [`ToOwned`] - if the `alloc` feature is enabled, this is an alias for //! `alloc::borrow::ToOwned`. -//! * [`Error`] - if the `std` feature is enabled, this is an alias for -//! `std::error::Error`. If the `std` feature is disabled, this is a trait -//! which is implemented for everything that implements [`Debug`] and -//! [`Display`]. Note that this means that enabling the `std` feature might -//! cause code that is designed carelessly to break due to no longer -//! implementing the trait. -//! -//! [`Debug`]: core::fmt::Debug -//! [`Display`]: core::fmt::Display - -#[cfg(not(feature = "std"))] -use core::fmt; #[cfg(feature = "alloc")] pub use rust_alloc::borrow::ToOwned; @@ -21,16 +9,6 @@ pub use rust_alloc::borrow::ToOwned; #[cfg(not(feature = "alloc"))] pub use self::to_owned::ToOwned; -#[cfg(feature = "std")] -pub use std::error::Error; - -/// Standard error trait used when the `std` feature is not enabled. -#[cfg(not(feature = "std"))] -pub trait Error: fmt::Debug + fmt::Display {} - -#[cfg(not(feature = "std"))] -impl Error for T where T: fmt::Debug + fmt::Display {} - #[cfg(not(feature = "alloc"))] mod to_owned { use core::borrow::Borrow; diff --git a/crates/musli-macros/Cargo.toml b/crates/musli-macros/Cargo.toml index 53b2da95b..ee7c50179 100644 --- a/crates/musli-macros/Cargo.toml +++ b/crates/musli-macros/Cargo.toml @@ -3,7 +3,7 @@ name = "musli-macros" version = "0.0.123" authors = ["John-John Tedro "] edition = "2021" -rust-version = "1.79" +rust-version = "1.81" description = """ Macros for Müsli. """ diff --git a/crates/musli-zerocopy-macros/Cargo.toml b/crates/musli-zerocopy-macros/Cargo.toml index 950d9c9bc..dc39cf29f 100644 --- a/crates/musli-zerocopy-macros/Cargo.toml +++ b/crates/musli-zerocopy-macros/Cargo.toml @@ -3,7 +3,7 @@ name = "musli-zerocopy-macros" version = "0.0.123" authors = ["John-John Tedro "] edition = "2021" -rust-version = "1.79" +rust-version = "1.81" description = """ Macros for Müsli zero-copy. """ diff --git a/crates/musli-zerocopy/Cargo.toml b/crates/musli-zerocopy/Cargo.toml index bc060ba33..19347677c 100644 --- a/crates/musli-zerocopy/Cargo.toml +++ b/crates/musli-zerocopy/Cargo.toml @@ -3,7 +3,7 @@ name = "musli-zerocopy" version = "0.0.123" authors = ["John-John Tedro "] edition = "2021" -rust-version = "1.79" +rust-version = "1.81" description = """ Refreshingly simple zero copy primitives by Müsli. """ diff --git a/crates/musli-zerocopy/src/error.rs b/crates/musli-zerocopy/src/error.rs index a5c3aeb1b..add9187fe 100644 --- a/crates/musli-zerocopy/src/error.rs +++ b/crates/musli-zerocopy/src/error.rs @@ -131,9 +131,8 @@ impl fmt::Display for Error { } } -#[cfg(feature = "std")] -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for Error { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match &self.kind { ErrorKind::Utf8Error { error } => Some(error), _ => None, diff --git a/crates/musli/Cargo.toml b/crates/musli/Cargo.toml index 163ed5cc8..5ca944881 100644 --- a/crates/musli/Cargo.toml +++ b/crates/musli/Cargo.toml @@ -3,7 +3,7 @@ name = "musli" version = "0.0.123" authors = ["John-John Tedro "] edition = "2021" -rust-version = "1.79" +rust-version = "1.81" description = """ Müsli is a flexible and generic binary serialization framework. """ diff --git a/crates/musli/src/context/capture.rs b/crates/musli/src/context/capture.rs index 39b801ec3..323983c15 100644 --- a/crates/musli/src/context/capture.rs +++ b/crates/musli/src/context/capture.rs @@ -1,11 +1,11 @@ use core::cell::UnsafeCell; +use core::error::Error; use core::fmt; use core::marker::PhantomData; use crate::alloc::{self, Allocator, String}; #[cfg(feature = "alloc")] use crate::alloc::{System, SYSTEM}; -use crate::no_std; use crate::Context; use super::{ContextError, ErrorMarker}; @@ -92,7 +92,7 @@ where #[inline] fn custom(&self, error: T) -> ErrorMarker where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + Error, { // SAFETY: We're restricting access to the context, so that this is // safe. diff --git a/crates/musli/src/context/context_error.rs b/crates/musli/src/context/context_error.rs index fba57bb63..a6f20a74e 100644 --- a/crates/musli/src/context/context_error.rs +++ b/crates/musli/src/context/context_error.rs @@ -5,10 +5,9 @@ //! encoding implementations to raise custom errors based on types that //! implement [Display][core::fmt::Display]. +use core::error::Error; use core::fmt; -use crate::no_std; - #[cfg(feature = "alloc")] use rust_alloc::string::{String, ToString}; @@ -22,7 +21,7 @@ pub trait ContextError: Sized + 'static + Send + Sync + fmt::Display + fmt::Debu /// Construct a custom error. fn custom(error: T) -> Self where - T: 'static + Send + Sync + no_std::Error; + T: 'static + Send + Sync + Error; /// Collect an error from something that can be displayed. /// @@ -33,20 +32,20 @@ pub trait ContextError: Sized + 'static + Send + Sync + fmt::Display + fmt::Debu T: fmt::Display; } -#[cfg(all(feature = "std", feature = "alloc"))] +#[cfg(feature = "std")] impl ContextError for std::io::Error { fn custom(message: T) -> Self where - T: 'static + Send + Sync + fmt::Display + fmt::Debug, + T: 'static + Send + Sync + Error, { - std::io::Error::new(std::io::ErrorKind::Other, message.to_string()) + std::io::Error::new(std::io::ErrorKind::Other, message) } fn message(message: T) -> Self where T: fmt::Display, { - std::io::Error::new(std::io::ErrorKind::Other, message.to_string()) + std::io::Error::new(std::io::ErrorKind::Other, std::format!("{message}")) } } diff --git a/crates/musli/src/context/error_marker.rs b/crates/musli/src/context/error_marker.rs index 8ebb56674..9dabcef3a 100644 --- a/crates/musli/src/context/error_marker.rs +++ b/crates/musli/src/context/error_marker.rs @@ -17,8 +17,7 @@ impl fmt::Display for ErrorMarker { } } -#[cfg(feature = "std")] -impl std::error::Error for ErrorMarker {} +impl core::error::Error for ErrorMarker {} #[cfg(test)] impl crate::context::ContextError for ErrorMarker { diff --git a/crates/musli/src/context/same.rs b/crates/musli/src/context/same.rs index c58a70144..14d3c9bf1 100644 --- a/crates/musli/src/context/same.rs +++ b/crates/musli/src/context/same.rs @@ -1,3 +1,4 @@ +use core::error::Error; use core::fmt; use core::marker::PhantomData; @@ -6,7 +7,6 @@ use crate::alloc::{self, Allocator, String}; use crate::alloc::{System, SYSTEM}; #[cfg(test)] use crate::mode::Binary; -use crate::no_std; use crate::Context; use super::ContextError; @@ -90,7 +90,7 @@ where #[inline] fn custom(&self, message: T) -> Self::Error where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + Error, { E::custom(message) } diff --git a/crates/musli/src/descriptive/error.rs b/crates/musli/src/descriptive/error.rs index 212043b2c..066b449fb 100644 --- a/crates/musli/src/descriptive/error.rs +++ b/crates/musli/src/descriptive/error.rs @@ -6,7 +6,6 @@ use rust_alloc::boxed::Box; use rust_alloc::string::ToString; use crate::context::ContextError; -use crate::no_std; /// Error raised during descriptive encoding. #[derive(Debug)] @@ -26,7 +25,7 @@ enum ErrorImpl { #[cfg(feature = "alloc")] Message(Box), #[cfg(feature = "alloc")] - Custom(Box), + Custom(Box), #[cfg(not(feature = "alloc"))] Empty, } @@ -44,11 +43,11 @@ impl fmt::Display for ErrorImpl { } } -#[cfg(all(feature = "std", feature = "alloc"))] -impl std::error::Error for Error { +impl core::error::Error for Error { #[inline] - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match &self.err { + #[cfg(feature = "alloc")] ErrorImpl::Custom(err) => Some(&**err), _ => None, } @@ -60,7 +59,7 @@ impl ContextError for Error { #[allow(unused_variables)] fn custom(error: T) -> Self where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + core::error::Error, { Self { #[cfg(feature = "alloc")] diff --git a/crates/musli/src/json/error.rs b/crates/musli/src/json/error.rs index e69bdf613..79667371e 100644 --- a/crates/musli/src/json/error.rs +++ b/crates/musli/src/json/error.rs @@ -39,8 +39,7 @@ impl fmt::Display for ErrorImpl { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl ContextError for Error { #[inline] @@ -82,8 +81,7 @@ impl fmt::Display for ErrorMessage { } } -#[cfg(feature = "std")] -impl std::error::Error for ErrorMessage {} +impl core::error::Error for ErrorMessage {} #[derive(Debug)] #[non_exhaustive] diff --git a/crates/musli/src/lib.rs b/crates/musli/src/lib.rs index 7da159a27..af4d93c2b 100644 --- a/crates/musli/src/lib.rs +++ b/crates/musli/src/lib.rs @@ -261,7 +261,7 @@ //! # struct Version1 { name: String } //! # #[derive(Debug, PartialEq, Encode, Decode)] //! # struct Version2 { name: String, #[musli(default)] age: Option } -//! # fn main() -> Result<(), Box> { +//! # fn main() -> Result<(), Box> { //! let version2 = musli::storage::to_vec(&Version2 { //! name: String::from("Aristotle"), //! age: Some(61), diff --git a/crates/musli/src/no_std.rs b/crates/musli/src/no_std.rs index ddd27808d..a2da164e8 100644 --- a/crates/musli/src/no_std.rs +++ b/crates/musli/src/no_std.rs @@ -2,18 +2,9 @@ //! //! * [`ToOwned`] - if the `alloc` feature is enabled, this is an alias for //! `alloc::borrow::ToOwned`. -//! * [`Error`] - if the `std` feature is enabled, this is an alias for -//! `std::error::Error`. If the `std` feature is disabled, this is a trait -//! which is implemented for everything that implements [`Debug`] and -//! [`Display`]. Note that this means that enabling the `std` feature might -//! cause code that is designed carelessly to break due to no longer -//! implementing the trait. -//! -//! [`Debug`]: core::fmt::Debug -//! [`Display`]: core::fmt::Display #[doc(inline)] -pub use musli_core::no_std::{Error, ToOwned}; +pub use musli_core::no_std::ToOwned; /// A somewhat portable, but also noisy abort implementation for no_std /// environments. diff --git a/crates/musli/src/reader.rs b/crates/musli/src/reader.rs index 8b8a47b1b..1ad55de7a 100644 --- a/crates/musli/src/reader.rs +++ b/crates/musli/src/reader.rs @@ -603,5 +603,4 @@ impl fmt::Display for SliceUnderflow { } } -#[cfg(feature = "std")] -impl std::error::Error for SliceUnderflow {} +impl core::error::Error for SliceUnderflow {} diff --git a/crates/musli/src/serde/mod.rs b/crates/musli/src/serde/mod.rs index 4e63756ca..c6d8825da 100644 --- a/crates/musli/src/serde/mod.rs +++ b/crates/musli/src/serde/mod.rs @@ -84,7 +84,7 @@ //! assert_eq!(musli.address.city, "Springfield"); //! assert_eq!(musli.address.zip, 12345); //! assert_eq!(musli.url, "https://example.com/"); -//! # Ok::<_, Box>(()) +//! # Ok::<_, Box>(()) //! ``` #![cfg(feature = "serde")] @@ -95,6 +95,7 @@ mod error; mod serializer; use core::cell::RefCell; +use core::error::Error; use core::fmt; use serde::{Deserialize, Serialize}; @@ -103,7 +104,6 @@ use self::deserializer::Deserializer; use self::serializer::Serializer; use crate::alloc::{self, String}; -use crate::no_std; use crate::{Context, Decoder, Encoder}; struct SerdeContext<'a, C> @@ -153,7 +153,7 @@ where #[inline] fn custom(&self, error: T) -> Self::Error where - T: 'static + Send + Sync + no_std::Error, + T: 'static + Send + Sync + Error, { *self.error.borrow_mut() = Some(self.inner.custom(error)); error::SerdeError::Captured diff --git a/crates/musli/src/storage/error.rs b/crates/musli/src/storage/error.rs index 92ba532d2..5d00149ae 100644 --- a/crates/musli/src/storage/error.rs +++ b/crates/musli/src/storage/error.rs @@ -39,8 +39,7 @@ impl fmt::Display for ErrorImpl { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl ContextError for Error { #[inline] diff --git a/crates/musli/src/str.rs b/crates/musli/src/str.rs index 26d7929ec..8ac3a1219 100644 --- a/crates/musli/src/str.rs +++ b/crates/musli/src/str.rs @@ -19,17 +19,13 @@ use core::fmt; #[cfg(not(feature = "simdutf8"))] #[doc(inline)] pub use core::str::from_utf8; -#[cfg(feature = "simdutf8")] -#[doc(inline)] -pub use simdutf8::basic::from_utf8; /// Error raised in case the UTF-8 sequence could not be decoded. -#[non_exhaustive] #[derive(Debug)] +#[non_exhaustive] pub struct Utf8Error; -#[cfg(feature = "std")] -impl std::error::Error for Utf8Error {} +impl core::error::Error for Utf8Error {} impl fmt::Display for Utf8Error { #[inline] @@ -58,10 +54,25 @@ pub fn from_utf8_owned(bytes: Vec) -> Result { #[inline(always)] #[cfg(all(feature = "alloc", feature = "simdutf8"))] pub fn from_utf8_owned(bytes: Vec) -> Result { - if from_utf8(&bytes).is_err() { + if simdutf8::basic::from_utf8(&bytes).is_err() { return Err(Utf8Error); } // SAFETY: String was checked above. Ok(unsafe { String::from_utf8_unchecked(bytes) }) } + +/// Analogue to [`core::str::from_utf8()`]. +/// +/// Checks if the passed byte sequence is valid UTF-8 and returns an +/// [`std::str`] reference to the passed byte slice wrapped in `Ok()` if it is. +/// +/// # Errors +/// +/// Will return the zero-sized Err([`Utf8Error`]) on if the input contains +/// invalid UTF-8. +#[inline] +#[cfg(feature = "simdutf8")] +pub fn from_utf8(input: &[u8]) -> Result<&str, Utf8Error> { + simdutf8::basic::from_utf8(input).map_err(|_| Utf8Error) +} diff --git a/crates/musli/src/value/error.rs b/crates/musli/src/value/error.rs index 18773269c..113162dd2 100644 --- a/crates/musli/src/value/error.rs +++ b/crates/musli/src/value/error.rs @@ -45,8 +45,7 @@ impl fmt::Display for ErrorImpl { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl ContextError for Error { #[inline] diff --git a/crates/musli/src/wire/error.rs b/crates/musli/src/wire/error.rs index 805963be4..941e66912 100644 --- a/crates/musli/src/wire/error.rs +++ b/crates/musli/src/wire/error.rs @@ -39,8 +39,7 @@ impl fmt::Display for ErrorImpl { } } -#[cfg(feature = "std")] -impl std::error::Error for Error {} +impl core::error::Error for Error {} impl ContextError for Error { #[inline] diff --git a/crates/musli/src/wire/mod.rs b/crates/musli/src/wire/mod.rs index 570c97bd5..06f8b7481 100644 --- a/crates/musli/src/wire/mod.rs +++ b/crates/musli/src/wire/mod.rs @@ -28,7 +28,7 @@ //! age: Option, //! } //! -//! # fn main() -> Result<(), Box> { +//! # fn main() -> Result<(), Box> { //! let version2 = musli::wire::to_vec(&Version2 { //! name: String::from("Aristotle"), //! age: Some(61), diff --git a/crates/musli/tests/default_value.rs b/crates/musli/tests/default_value.rs index 9e2111eed..472659168 100644 --- a/crates/musli/tests/default_value.rs +++ b/crates/musli/tests/default_value.rs @@ -68,7 +68,7 @@ fn default_age() -> u32 { // Ensure that skipped over fields ensures compatibility. #[test] -fn decode_with_default() -> Result<(), Box> { +fn decode_with_default() -> Result<(), Box> { static NAME: &str = "Aristotle"; static COUNTRY: &str = "Greece"; diff --git a/crates/musli/tests/untagged_enums.rs b/crates/musli/tests/untagged_enums.rs index fdbfb2e39..324928684 100644 --- a/crates/musli/tests/untagged_enums.rs +++ b/crates/musli/tests/untagged_enums.rs @@ -40,7 +40,7 @@ pub struct EmptyVariant; /// Untagged enums may only implement `Encode`, and will be encoded according to /// the exact specification of fields part of the variant. #[test] -fn untagged_enums() -> Result<(), Box> { +fn untagged_enums() -> Result<(), Box> { musli::macros::assert_decode_eq! { full, Enum::EmptyVariant1, diff --git a/tests-macros/Cargo.toml b/tests-macros/Cargo.toml index 90dd3cbd6..ab4fa098d 100644 --- a/tests-macros/Cargo.toml +++ b/tests-macros/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "tests-macros" edition = "2021" -rust-version = "1.79" +rust-version = "1.81" publish = false [lib]