Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to core::error::Error #178

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "musli-core"
version = "0.0.123"
authors = ["John-John Tedro <udoprog@tedro.se>"]
edition = "2021"
rust-version = "1.79"
rust-version = "1.81"
description = """
Core traits and types for the Müsli serialization framework.
"""
Expand Down
8 changes: 4 additions & 4 deletions crates/musli-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -83,7 +83,7 @@ pub trait Context {
#[inline]
fn map<T>(&self) -> impl FnOnce(T) -> Self::Error + '_
where
T: 'static + Send + Sync + no_std::Error,
T: 'static + Send + Sync + Error,
{
move |error| self.custom(error)
}
Expand All @@ -93,7 +93,7 @@ pub trait Context {
/// reporting error-like things out from the context.
fn custom<T>(&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]
Expand Down Expand Up @@ -131,7 +131,7 @@ pub trait Context {
#[inline(always)]
fn marked_custom<T>(&self, mark: Self::Mark, message: T) -> Self::Error
where
T: 'static + Send + Sync + no_std::Error,
T: 'static + Send + Sync + Error,
{
self.custom(message)
}
Expand Down
3 changes: 1 addition & 2 deletions crates/musli-core/src/internal/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, const N: usize> {
Expand Down
22 changes: 0 additions & 22 deletions crates/musli-core/src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,13 @@
//!
//! * [`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;

#[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<T> Error for T where T: fmt::Debug + fmt::Display {}

#[cfg(not(feature = "alloc"))]
mod to_owned {
use core::borrow::Borrow;
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "musli-macros"
version = "0.0.123"
authors = ["John-John Tedro <udoprog@tedro.se>"]
edition = "2021"
rust-version = "1.79"
rust-version = "1.81"
description = """
Macros for Müsli.
"""
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "musli-zerocopy-macros"
version = "0.0.123"
authors = ["John-John Tedro <udoprog@tedro.se>"]
edition = "2021"
rust-version = "1.79"
rust-version = "1.81"
description = """
Macros for Müsli zero-copy.
"""
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-zerocopy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "musli-zerocopy"
version = "0.0.123"
authors = ["John-John Tedro <udoprog@tedro.se>"]
edition = "2021"
rust-version = "1.79"
rust-version = "1.81"
description = """
Refreshingly simple zero copy primitives by Müsli.
"""
Expand Down
5 changes: 2 additions & 3 deletions crates/musli-zerocopy/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/musli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "musli"
version = "0.0.123"
authors = ["John-John Tedro <udoprog@tedro.se>"]
edition = "2021"
rust-version = "1.79"
rust-version = "1.81"
description = """
Müsli is a flexible and generic binary serialization framework.
"""
Expand Down
4 changes: 2 additions & 2 deletions crates/musli/src/context/capture.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -92,7 +92,7 @@ where
#[inline]
fn custom<T>(&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.
Expand Down
13 changes: 6 additions & 7 deletions crates/musli/src/context/context_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -22,7 +21,7 @@ pub trait ContextError: Sized + 'static + Send + Sync + fmt::Display + fmt::Debu
/// Construct a custom error.
fn custom<T>(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.
///
Expand All @@ -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<T>(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<T>(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}"))
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/musli/src/context/error_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/musli/src/context/same.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::error::Error;
use core::fmt;
use core::marker::PhantomData;

Expand All @@ -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;
Expand Down Expand Up @@ -90,7 +90,7 @@ where
#[inline]
fn custom<T>(&self, message: T) -> Self::Error
where
T: 'static + Send + Sync + no_std::Error,
T: 'static + Send + Sync + Error,
{
E::custom(message)
}
Expand Down
11 changes: 5 additions & 6 deletions crates/musli/src/descriptive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -26,7 +25,7 @@ enum ErrorImpl {
#[cfg(feature = "alloc")]
Message(Box<str>),
#[cfg(feature = "alloc")]
Custom(Box<dyn 'static + Send + Sync + no_std::Error>),
Custom(Box<dyn 'static + Send + Sync + core::error::Error>),
#[cfg(not(feature = "alloc"))]
Empty,
}
Expand All @@ -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,
}
Expand All @@ -60,7 +59,7 @@ impl ContextError for Error {
#[allow(unused_variables)]
fn custom<T>(error: T) -> Self
where
T: 'static + Send + Sync + no_std::Error,
T: 'static + Send + Sync + core::error::Error,
{
Self {
#[cfg(feature = "alloc")]
Expand Down
6 changes: 2 additions & 4 deletions crates/musli/src/json/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/musli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
//! # struct Version1 { name: String }
//! # #[derive(Debug, PartialEq, Encode, Decode)]
//! # struct Version2 { name: String, #[musli(default)] age: Option<u32> }
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! let version2 = musli::storage::to_vec(&Version2 {
//! name: String::from("Aristotle"),
//! age: Some(61),
Expand Down
11 changes: 1 addition & 10 deletions crates/musli/src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions crates/musli/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,5 +603,4 @@ impl fmt::Display for SliceUnderflow {
}
}

#[cfg(feature = "std")]
impl std::error::Error for SliceUnderflow {}
impl core::error::Error for SliceUnderflow {}
6 changes: 3 additions & 3 deletions crates/musli/src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn std::error::Error>>(())
//! # Ok::<_, Box<dyn core::error::Error>>(())
//! ```

#![cfg(feature = "serde")]
Expand All @@ -95,6 +95,7 @@ mod error;
mod serializer;

use core::cell::RefCell;
use core::error::Error;
use core::fmt;

use serde::{Deserialize, Serialize};
Expand All @@ -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>
Expand Down Expand Up @@ -153,7 +153,7 @@ where
#[inline]
fn custom<T>(&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
Expand Down
3 changes: 1 addition & 2 deletions crates/musli/src/storage/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading