Skip to content

Commit

Permalink
Rework feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Mar 18, 2024
1 parent 0907aeb commit 7b6f590
Show file tree
Hide file tree
Showing 34 changed files with 168 additions and 102 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo build -p ${{matrix.package}} --no-default-features
- run: cargo build -p ${{matrix.package}} --no-default-features --features alloc
- run: cargo build -p ${{matrix.package}} --no-default-features --features std
- run: cargo build -p ${{matrix.package}} --no-default-features --features simdutf8
- run: cargo build -p ${{matrix.package}} --no-default-features -- -D warnings
- run: cargo build -p ${{matrix.package}} --no-default-features --features alloc -- -D warnings
- run: cargo build -p ${{matrix.package}} --no-default-features --features std -- -D warnings
- run: cargo build -p ${{matrix.package}} --no-default-features --features simdutf8 -- -D warnings
if: matrix.package == 'musli-storage' || matrix.package == 'musli-wire' || matrix.package == 'musli-descriptive' || matrix.package == 'musli-json'
- run: cargo build -p ${{matrix.package}} --no-default-features --features parse-full
- run: cargo build -p ${{matrix.package}} --no-default-features --features parse-full -- -D warnings
if: matrix.package == 'musli-json'

clippy:
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-allocator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ keywords = ["no-std::no-alloc", "no_std", "serialization"]
categories = ["memory-management", "no-std", "rust-patterns"]

[features]
default = ["std"]
std = ["alloc", "musli/std"]
default = ["std", "alloc"]
std = ["musli/std"]
alloc = ["musli/alloc"]

[dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ keywords = ["no-std::no-alloc", "no_std", "serialization"]
categories = ["encoding", "no-std"]

[features]
default = ["std"]
std = ["alloc", "musli/std", "simdutf8?/std", "musli-allocator/std"]
default = ["std", "alloc"]
std = ["musli/std", "simdutf8?/std", "musli-allocator/std"]
alloc = ["musli-allocator/alloc"]

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions crates/musli-common/src/buf/buf_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use musli::Buf;

use crate::fixed::CapacityError;

/// A string around a buffer.
pub(crate) struct BufString<B> {
/// A string wrapped around a context buffer.
pub struct BufString<B> {
buf: B,
}

Expand All @@ -16,7 +16,7 @@ where
B: Buf,
{
/// Construct a new fixed string.
pub(crate) const fn new(buf: B) -> BufString<B> {
pub const fn new(buf: B) -> BufString<B> {
BufString { buf }
}

Expand Down
4 changes: 3 additions & 1 deletion crates/musli-common/src/buf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
//! Utilitioes for working with buffers.

mod buf_string;
pub(crate) use self::buf_string::BufString;
pub use self::buf_string::BufString;
6 changes: 4 additions & 2 deletions crates/musli-common/src/context/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use core::fmt;

use musli::context::StdError;

#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};

Expand All @@ -15,7 +17,7 @@ pub trait Error: Sized + 'static + Send + Sync + fmt::Display + fmt::Debug {
/// Construct a custom error.
fn custom<T>(error: T) -> Self
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug;
T: 'static + Send + Sync + StdError;

/// Collect an error from something that can be displayed.
///
Expand All @@ -26,7 +28,7 @@ pub trait Error: Sized + 'static + Send + Sync + fmt::Display + fmt::Debug {
T: fmt::Display;
}

#[cfg(feature = "std")]
#[cfg(all(feature = "std", feature = "alloc"))]
impl Error for std::io::Error {
fn custom<T>(message: T) -> Self
where
Expand Down
7 changes: 4 additions & 3 deletions crates/musli-common/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use core::cell::{Cell, UnsafeCell};
use core::fmt;
use core::marker::PhantomData;

use musli::context::StdError;
use musli::mode::DefaultMode;
use musli::{Allocator, Context};

Expand Down Expand Up @@ -84,7 +85,7 @@ where
#[inline]
fn custom<T>(&self, message: T) -> Self::Error
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
T: 'static + Send + Sync + StdError,
{
E::custom(message)
}
Expand Down Expand Up @@ -146,7 +147,7 @@ where
/// Construct an error or panic.
pub fn unwrap(self) -> E {
if self.error.get() {
return E::custom("error");
return E::message("error");
}

panic!("did not error")
Expand Down Expand Up @@ -231,7 +232,7 @@ where
#[inline]
fn custom<T>(&self, error: T) -> ErrorMarker
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
T: 'static + Send + Sync + StdError,
{
// SAFETY: We're restricting access to the context, so that this is
// safe.
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-common/src/fixed_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<const N: usize> FixedBytes<N> {
C: ?Sized + Context,
{
if !self.extend_from_slice(source) {
return Err(cx.custom(FixedBytesOverflow {
return Err(cx.message(FixedBytesOverflow {
at: self.init,
additional: source.len(),
capacity: N,
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-common/src/int/continuation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
shift += 7;

if shift >= T::BITS {
return Err(cx.custom("Bits overflow"));
return Err(cx.message("Bits overflow"));
}

b = r.read_byte(cx)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub mod fixed_bytes;
pub mod int;
#[macro_use]
pub mod options;
mod buf;
pub mod buf;
pub mod reader;
pub mod str;
pub mod wrap;
Expand Down
6 changes: 3 additions & 3 deletions crates/musli-common/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl<'de> Reader<'de> for &'de [u8] {
C: ?Sized + Context,
{
if self.len() < n {
return Err(cx.custom(SliceUnderflow {
return Err(cx.message(SliceUnderflow {
n,
remaining: self.len(),
}));
Expand Down Expand Up @@ -348,7 +348,7 @@ where
let outcome = range.start.wrapping_add(len);

if outcome > range.end || outcome < range.start {
Err(cx.custom(SliceUnderflow {
Err(cx.message(SliceUnderflow {
n: len,
remaining: (range.end as usize).wrapping_sub(range.start as usize),
}))
Expand Down Expand Up @@ -385,7 +385,7 @@ where
self.remaining = remaining;
Ok(())
}
None => Err(cx.custom("Reader out of bounds")),
None => Err(cx.message("Reader out of bounds")),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-common/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Writer for &mut [u8] {
C: ?Sized + Context,
{
if self.len() < bytes.len() {
return Err(cx.custom(SliceOverflow {
return Err(cx.message(SliceOverflow {
n: bytes.len(),
capacity: self.len(),
}));
Expand Down
4 changes: 2 additions & 2 deletions crates/musli-descriptive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ keywords = ["no-std::no-alloc", "no_std", "serialization"]
categories = ["encoding", "no-std"]

[features]
default = ["std", "simdutf8"]
std = ["alloc", "musli/std", "musli-common/std", "musli-storage/std"]
default = ["std", "alloc", "simdutf8"]
std = ["musli/std", "musli-common/std", "musli-storage/std"]
alloc = ["musli/alloc", "musli-common/alloc", "musli-storage/alloc"]
test = []
simdutf8 = ["musli-common/simdutf8"]
Expand Down
34 changes: 27 additions & 7 deletions crates/musli-descriptive/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use alloc::string::ToString;

use musli::context::StdError;

/// Error raised during descriptive encoding.
#[derive(Debug)]
pub struct Error {
Expand All @@ -22,31 +24,49 @@ impl fmt::Display for Error {
enum ErrorImpl {
#[cfg(feature = "alloc")]
Message(Box<str>),
#[cfg(feature = "alloc")]
Custom(Box<dyn 'static + Send + Sync + StdError>),
#[cfg(not(feature = "alloc"))]
Message,
Empty,
}

impl fmt::Display for ErrorImpl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "alloc")]
ErrorImpl::Message(message) => message.fmt(f),
#[cfg(feature = "alloc")]
ErrorImpl::Custom(message) => message.fmt(f),
#[cfg(not(feature = "alloc"))]
ErrorImpl::Message => write!(f, "Message error (see diagnostics)"),
ErrorImpl::Empty => write!(f, "Message error (see diagnostics)"),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[cfg(all(feature = "std", feature = "alloc"))]
impl std::error::Error for Error {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.err {
ErrorImpl::Custom(err) => Some(&**err),
_ => None,
}
}
}

impl musli_common::context::Error for Error {
#[inline]
#[allow(unused_variables)]
fn custom<T>(error: T) -> Self
where
T: fmt::Display,
T: 'static + Send + Sync + StdError,
{
Self::message(error)
Self {
#[cfg(feature = "alloc")]
err: ErrorImpl::Custom(Box::new(error)),
#[cfg(not(feature = "alloc"))]
err: ErrorImpl::Empty,
}
}

#[inline]
Expand All @@ -59,7 +79,7 @@ impl musli_common::context::Error for Error {
#[cfg(feature = "alloc")]
err: ErrorImpl::Message(message.to_string().into()),
#[cfg(not(feature = "alloc"))]
err: ErrorImpl::Message,
err: ErrorImpl::Empty,
}
}
}
2 changes: 1 addition & 1 deletion crates/musli-json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keywords = ["no-std::no-alloc", "no_std", "serialization"]
categories = ["encoding", "no-std"]

[features]
default = ["std", "simdutf8", "musli-value", "parse-full"]
default = ["std", "alloc", "simdutf8", "musli-value", "parse-full"]
std = ["musli/std", "musli-common/std", "musli-value?/std", "alloc", "lexical/std"]
alloc = ["musli/alloc", "musli-common/alloc", "musli-value?/alloc"]
test = []
Expand Down
17 changes: 13 additions & 4 deletions crates/musli-json/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ impl fmt::Display for Error {
}

#[derive(Debug)]
pub(crate) enum ErrorImpl {
enum ErrorImpl {
#[cfg(feature = "alloc")]
Message(Box<str>),
#[cfg(not(feature = "alloc"))]
Message,
Empty,
}

impl fmt::Display for ErrorImpl {
Expand All @@ -32,7 +32,7 @@ impl fmt::Display for ErrorImpl {
#[cfg(feature = "alloc")]
ErrorImpl::Message(message) => message.fmt(f),
#[cfg(not(feature = "alloc"))]
ErrorImpl::Message => write!(f, "Message error (see diagnostics)"),
ErrorImpl::Empty => write!(f, "Message error (see diagnostics)"),
}
}
}
Expand All @@ -59,7 +59,7 @@ impl musli_common::context::Error for Error {
#[cfg(feature = "alloc")]
err: ErrorImpl::Message(message.to_string().into()),
#[cfg(not(feature = "alloc"))]
err: ErrorImpl::Message,
err: ErrorImpl::Empty,
}
}
}
Expand All @@ -80,6 +80,15 @@ impl fmt::Display for ErrorMessage {
}
}

#[cfg(feature = "std")]
impl std::error::Error for ErrorMessage {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ErrorMessage::ParseFloat(error) => Some(error),
}
}
}

#[derive(Debug)]
#[non_exhaustive]
pub(crate) enum IntegerError {
Expand Down
Loading

0 comments on commit 7b6f590

Please sign in to comment.