Skip to content

Commit

Permalink
Refactor, disable num feature
Browse files Browse the repository at this point in the history
  • Loading branch information
RoDmitry committed Dec 25, 2023
1 parent 7895f00 commit 492d4f8
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: cargo clippy --verbose -- -Dwarnings

- name: Std
run: cargo test --verbose
run: cargo test --verbose --all-features --workspace

- name: Prepare
# gcc-multilib is needed for i686
Expand All @@ -41,4 +41,4 @@ jobs:
- name: Install i686 target
run: rustup target add i686-unknown-linux-gnu
- name: i686 (32 bits)
run: cargo test --target=i686-unknown-linux-gnu --verbose
run: cargo test --target=i686-unknown-linux-gnu --verbose --all-features --workspace
35 changes: 22 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
[package]
description = "Merge structs into single by values"
exclude = [".github/*"]
keywords = ["struct", "merge", "combine", "macros", "derive"]
name = "merge2"
readme = "README.md"
version = "0.1.0"
authors.workspace = true
categories.workspace = true
documentation.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[workspace.package]
authors = ["Dmitry Rodionov <gh@rdmtr.com>"]
categories = ["rust-patterns"]
documentation = "https://docs.rs/merge2/"
edition = "2021"
description = "Merge structs into single by values"
license = "MIT OR Apache-2.0"
repository = "https://github.com/RoDmitry/merge2"
keywords = ["struct", "merge", "combine", "macros", "derive"]
categories = ["rust-patterns"]
license = "Apache-2.0 OR MIT"
exclude = [".builds/*"]
readme = "README.md"

[lib]
name = "merge2"
path = "src/lib.rs"

[dependencies]
merge2_derive = { path = "merge2_derive", version = "0.1.0", optional = true }
merge2_derive = { path = "merge2_derive", version = "0.1", optional = true }
num-traits = { version = "0.2.12", optional = true }

[dev-dependencies]
envy = "0.4"
toml = "0.8"
trybuild = "1.0"
structopt = "0.3"
serde = { version = "1.0", features = ["derive"] }

[features]
default = ["derive", "num", "std"]
default = ["derive", "std"]
derive = ["merge2_derive"]
num = ["num-traits"]
std = []

[workspace]
members = ["merge2_derive"]
members = ["merge2_derive", "merge2_examples"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Provides `Merge` trait that can be used to merge structs into single by it's values:

```rust
trait Merge {
trait Merge: Sized {
fn merge(&mut self, other: &mut Self);
}
```
Expand Down
13 changes: 8 additions & 5 deletions merge2_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
[package]
description = "Derive macro for Merge2"
name = "merge2_derive"
version = "0.1.0"
authors = ["Dmitry Rodionov <gh@rdmtr.com>"]
edition = "2021"
license = "Apache-2.0 OR MIT"
publish = false
authors.workspace = true
categories.workspace = true
documentation.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0"
manyhow = "0.10"
proc-macro2 = "1.0"
quote = "1.0"
syn = "2.0"
13 changes: 13 additions & 0 deletions merge2_examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
description = "Merge2 examples"
edition = "2021"
name = "merge2_examples"
version = "0.1.0"
publish = false

[dependencies]
merge2 = { path = ".." }
clap = { version = "4.4", default-features = false, features = ["derive", "std"] }
envy = "0.4"
serde = { version = "1.0", features = ["derive"] }
toml = "0.8"
8 changes: 4 additions & 4 deletions examples/args.rs → merge2_examples/examples/args.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use clap::Parser;
use merge2::Merge;
use serde::Deserialize;
use structopt::StructOpt;

#[derive(Debug, Default, Deserialize, Merge, StructOpt)]
#[derive(Debug, Default, Deserialize, Merge, Parser)]
#[serde(default)]
struct Args {
#[structopt(short, long)]
#[arg(short, long)]
#[merge(strategy = ::merge2::bool::overwrite_false)]
debug: bool,
input: Option<String>,
Expand All @@ -29,7 +29,7 @@ fn get_env() -> Args {
}

fn main() {
let mut args = Args::from_args();
let mut args = Args::parse();
args.merge(&mut get_env());
if let Some(mut config) = get_config() {
args.merge(&mut config);
Expand Down
File renamed without changes.
12 changes: 2 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Provides [`Merge`][] trait that can be used to merge structs into single by it's values:
//!
//! ```
//! trait Merge {
//! trait Merge: Sized {
//! fn merge(&mut self, other: &mut Self);
//! }
//! ```
Expand Down Expand Up @@ -148,7 +148,7 @@ pub use merge2_derive::*;
/// option3: None,
/// }, val);
/// ```
pub trait Merge {
pub trait Merge: Sized {
/// Merge another object into this object.
fn merge(&mut self, other: &mut Self);
}
Expand Down Expand Up @@ -247,14 +247,6 @@ pub mod num {
pub fn saturating_add<T: num_traits::SaturatingAdd>(left: &mut T, right: &mut T) {
*left = left.saturating_add(right);
}

/// Overwrite left with right if the value of left is zero.
#[inline]
pub fn overwrite_zero<T: num_traits::Zero + Copy>(left: &mut T, right: &mut T) {
if left.is_zero() {
*left = *right;
}
}
}

/// Merge strategies for types that form a total order.
Expand Down
2 changes: 1 addition & 1 deletion tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ fn test_unnamed_fields_skip() {
}

#[test]
#[cfg(feature = "std")]
#[cfg(all(feature = "num", feature = "std"))]
fn test_default_strategy() {
#[derive(Debug, Merge, PartialEq)]
struct N(#[merge(strategy = ::merge2::num::saturating_add)] u8);
Expand Down
15 changes: 3 additions & 12 deletions tests/strategies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_option_overwrite_none() {
}

#[test]
#[cfg(feature = "std")]
#[cfg(all(feature = "num", feature = "std"))]
fn test_option_recursive() {
#[derive(Debug, Merge, PartialEq)]
struct N(#[merge(strategy = ::merge2::num::saturating_add)] u8);
Expand Down Expand Up @@ -99,17 +99,6 @@ fn test_num_saturating_add() {
test(S(40), S(30), S(10));
}

#[cfg(feature = "num")]
#[test]
fn test_num_overwrite_zero() {
#[derive(Debug, Merge, PartialEq)]
struct S(#[merge(strategy = ::merge2::num::overwrite_zero)] u8);

test(S(0), S(0), S(0));
test(S(1), S(0), S(1));
test(S(255), S(255), S(10));
}

#[test]
fn test_ord_max_def() {
#[derive(Debug, Merge, PartialEq)]
Expand Down Expand Up @@ -291,6 +280,7 @@ mod hashmap {
}

#[test]
#[cfg(feature = "num")]
fn test_recursive() {
#[derive(Debug, Merge, PartialEq)]
struct N(#[merge(strategy = ::merge2::num::saturating_add)] u8);
Expand All @@ -316,6 +306,7 @@ mod hashmap {
}

#[test]
#[cfg(feature = "num")]
fn test_intersection() {
#[derive(Debug, Merge, PartialEq)]
struct N(#[merge(strategy = ::merge2::num::saturating_add)] u8);
Expand Down

0 comments on commit 492d4f8

Please sign in to comment.