Skip to content

Commit

Permalink
fix up build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Dec 20, 2023
1 parent 44edecd commit f95b05b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nested/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ categories = ["encoding", "no-std"]
features = ["std"]

[features]
default = ["alloc"]
std = ["alloc", "sval/std", "sval_buffer/std"]
alloc = ["sval/alloc", "sval_buffer/alloc"]
no_debug_assertions = []

[dependencies.sval]
version = "2.10.2"
Expand Down
30 changes: 29 additions & 1 deletion nested/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ pub struct Error(ErrorKind);
#[derive(Debug)]
enum ErrorKind {
Buffer(sval_buffer::Error),
InvalidValue { reason: &'static str },
InvalidValue {
reason: &'static str,
},
#[cfg(not(feature = "alloc"))]
#[allow(dead_code)]
NoAlloc {
method: &'static str,
},
}

impl fmt::Display for Error {
Expand All @@ -21,6 +28,8 @@ impl fmt::Display for Error {
ErrorKind::InvalidValue { reason } => {
write!(f, "the value is invalid: {}", reason)
}
#[cfg(not(feature = "alloc"))]
ErrorKind::NoAlloc { method } => write!(f, "cannot allocate for {}", method),
}
}
}
Expand All @@ -36,6 +45,23 @@ impl Error {
pub fn invalid_value(reason: &'static str) -> Self {
Error(ErrorKind::InvalidValue { reason })
}

#[cfg(not(feature = "alloc"))]
#[track_caller]
pub(crate) fn no_alloc(method: &'static str) -> Self {
/*
The pattern here is the same as what's used in `sval_buffer`.
*/

#[cfg(all(debug_assertions, not(no_debug_assertions), not(test)))]
{
panic!("attempt to allocate for {} would fail; add the `alloc` feature of `sval_nested` or the depdendent `sval_*` library to support allocation. This call will error instead of panicking in release builds. Add the `no_debug_assertions` feature of `sval_nested` if this error is expected.", method);
}
#[cfg(not(all(debug_assertions, not(no_debug_assertions), not(test))))]
{
Error(ErrorKind::NoAlloc { method })
}
}
}

#[cfg(feature = "std")]
Expand All @@ -49,6 +75,8 @@ mod std_support {
match self.0 {
ErrorKind::Buffer(ref err) => Some(err),
ErrorKind::InvalidValue { .. } => None,
#[cfg(not(feature = "alloc"))]
ErrorKind::NoAlloc { .. } => None,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion nested/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ pub mod default_stream {
mod tests {
use super::*;

use alloc::borrow::Cow;
use std::borrow::Cow;

use sval_derive_macros::*;

Expand Down

0 comments on commit f95b05b

Please sign in to comment.