Skip to content

Commit

Permalink
Make it work in no_std environment (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy128k committed Apr 20, 2021
1 parent 7bea88f commit 57179c6
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Expand Up @@ -49,3 +49,9 @@ jobs:
cargo build ${{ matrix.features }} --verbose
cargo test --no-fail-fast ${{ matrix.features }} --verbose -- --nocapture
cargo doc
- name: no_std tests crate
run: |
cd derive_builder_no_std_tests
cargo build ${{ matrix.features }} --verbose
cargo test --no-fail-fast ${{ matrix.features }} --verbose -- --nocapture
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,2 +1,2 @@
[workspace]
members = ["derive_builder", "derive_builder_macro", "derive_builder_core"]
members = ["derive_builder", "derive_builder_macro", "derive_builder_core", "derive_builder_no_std_tests"]
5 changes: 5 additions & 0 deletions derive_builder/src/lib.rs
Expand Up @@ -541,6 +541,9 @@
#![deny(warnings)]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
extern crate alloc;

extern crate derive_builder_core;
extern crate derive_builder_macro;

Expand All @@ -552,6 +555,8 @@ pub use derive_builder_core::UninitializedFieldError;
#[doc(hidden)]
pub mod export {
pub mod core {
#[cfg(not(feature = "std"))]
pub use alloc::string;
#[cfg(not(feature = "std"))]
pub use core::*;
#[cfg(feature = "std")]
Expand Down
31 changes: 18 additions & 13 deletions derive_builder_core/src/builder.rs
Expand Up @@ -48,7 +48,7 @@ use Setter;
/// /// Uninitialized field
/// UninitializedField(&'static str),
/// /// Custom validation error
/// ValidationError(String),
/// ValidationError(::derive_builder::export::core::string::String),
/// }
///
/// impl ::derive_builder::export::core::convert::From<&'static str> for FooBuilderError {
Expand All @@ -57,8 +57,8 @@ use Setter;
/// }
/// }
///
/// impl ::derive_builder::export::core::convert::From<String> for FooBuilderError {
/// fn from(s: String) -> Self {
/// impl ::derive_builder::export::core::convert::From<::derive_builder::export::core::string::String> for FooBuilderError {
/// fn from(s: ::derive_builder::export::core::string::String) -> Self {
/// Self::ValidationError(s)
/// }
/// }
Expand Down Expand Up @@ -139,6 +139,8 @@ pub struct Builder<'a> {
pub doc_comment: Option<syn::Attribute>,
/// Emit deprecation notes to the user.
pub deprecation_notes: DeprecationNotes,
/// Whether or not a libstd is used.
pub std: bool,
}

impl<'a> ToTokens for Builder<'a> {
Expand Down Expand Up @@ -219,7 +221,7 @@ impl<'a> ToTokens for Builder<'a> {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(String),
ValidationError(::derive_builder::export::core::string::String),
}

impl ::derive_builder::export::core::convert::From<::derive_builder::UninitializedFieldError> for #builder_error_ident {
Expand All @@ -228,8 +230,8 @@ impl<'a> ToTokens for Builder<'a> {
}
}

impl ::derive_builder::export::core::convert::From<String> for #builder_error_ident {
fn from(s: String) -> Self {
impl ::derive_builder::export::core::convert::From<::derive_builder::export::core::string::String> for #builder_error_ident {
fn from(s: ::derive_builder::export::core::string::String) -> Self {
Self::ValidationError(s)
}
}
Expand All @@ -242,10 +244,13 @@ impl<'a> ToTokens for Builder<'a> {
}
}
}
));

#[cfg(not(no_std))]
impl std::error::Error for #builder_error_ident {}
));
if self.std {
tokens.append_all(quote!(
impl std::error::Error for #builder_error_ident {}
));
}
}
}
}
Expand Down Expand Up @@ -327,6 +332,7 @@ macro_rules! default_builder {
must_derive_clone: true,
doc_comment: None,
deprecation_notes: DeprecationNotes::default(),
std: true,
}
};
}
Expand All @@ -346,7 +352,7 @@ mod tests {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(String),
ValidationError(::derive_builder::export::core::string::String),
}

impl ::derive_builder::export::core::convert::From<::derive_builder::UninitializedFieldError> for FooBuilderError {
Expand All @@ -355,8 +361,8 @@ mod tests {
}
}

impl ::derive_builder::export::core::convert::From<String> for FooBuilderError {
fn from(s: String) -> Self {
impl ::derive_builder::export::core::convert::From<::derive_builder::export::core::string::String> for FooBuilderError {
fn from(s: ::derive_builder::export::core::string::String) -> Self {
Self::ValidationError(s)
}
}
Expand All @@ -370,7 +376,6 @@ mod tests {
}
}

#[cfg(not(no_std))]
impl std::error::Error for FooBuilderError {}
));
}
Expand Down
4 changes: 4 additions & 0 deletions derive_builder_core/src/macro_options/darling_opts.rs
Expand Up @@ -401,6 +401,10 @@ impl Options {
must_derive_clone: self.requires_clone(),
doc_comment: None,
deprecation_notes: Default::default(),
std: {
let no_std: bool = self.no_std.into();
!no_std
},
}
}

Expand Down
11 changes: 11 additions & 0 deletions derive_builder_no_std_tests/Cargo.toml
@@ -0,0 +1,11 @@
[package]
name = "derive_builder_no_std_tests"
version = "0.1.0"
authors = ["Andrey Kutejko <andy128k@gmail.com>"]
edition = "2018"

[features]
clippy = []

[dependencies]
derive_builder = { path = "../derive_builder", default-features = false }
18 changes: 18 additions & 0 deletions derive_builder_no_std_tests/src/main.rs
@@ -0,0 +1,18 @@
#![no_std]
#![allow(unused)]

#[macro_use]
extern crate derive_builder;

extern crate alloc;

#[derive(Builder)]
#[builder(no_std)]
struct Foo {
bar: i32,
}

fn main() {
let foo = FooBuilder::default().build();
assert!(foo.is_err());
}

0 comments on commit 57179c6

Please sign in to comment.