Skip to content
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
7 changes: 7 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
nix develop .#ci -c cargo run --no-default-features --example time
nix develop .#ci -c cargo run --no-default-features --example clap
nix develop .#ci -c cargo run --no-default-features --features=nesting --example nesting
nix develop .#ci -c cargo run --no-default-features --features=option --example option
nix develop .#ci -c cargo test --no-default-features

- name: Test with std features
Expand Down Expand Up @@ -66,3 +67,9 @@ jobs:
nix develop .#ci -c cargo run --features=nesting --example nesting
nix develop .#ci -c cargo run --features=nesting --example clap
nix develop .#ci -c cargo test

- name: Test in no std
run: |
cd no-std-examples
nix develop .#no-std -c cargo run --features=box --bin no-std-box
nix develop .#no-std -c cargo run --features=option --bin no-std-option
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Cargo.lock
.envrc
.direnv/
struct-patch/examples
no-std-examples/target
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"lib",
"derive",
]
exclude = [ "no-std-examples" ]

[workspace.package]
authors = ["Antonio Yang <yanganto@gmail.com>"]
Expand Down
1 change: 0 additions & 1 deletion derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,3 @@ fn get_lit(attr_name: String, meta: &ParseNestedMeta) -> syn::Result<Option<syn:
))
}
}

16 changes: 8 additions & 8 deletions derive/src/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use proc_macro2::{Ident, Span, TokenStream};
use quote::{quote, ToTokens};
use std::str::FromStr;
use syn::{
Lit,
meta::ParseNestedMeta, parenthesized, spanned::Spanned, DeriveInput, Error, LitStr, Result,
Type,
meta::ParseNestedMeta, parenthesized, spanned::Spanned, DeriveInput, Error, Lit, LitStr,
Result, Type,
};

#[cfg(feature = "op")]
Expand Down Expand Up @@ -148,13 +147,13 @@ impl Patch {
#[cfg(not(feature = "nesting"))]
let original_field_name_empty_values = fields
.iter()
.filter(|f| !f.retyped )
.filter(|f| !f.retyped)
.filter_map(|f| f.empty_value.as_ref())
.collect::<Vec<_>>();
#[cfg(feature = "nesting")]
let original_field_name_empty_values = fields
.iter()
.filter(|f| !f.retyped && !f.nesting )
.filter(|f| !f.retyped && !f.nesting)
.filter_map(|f| f.empty_value.as_ref())
.collect::<Vec<_>>();

Expand Down Expand Up @@ -824,11 +823,12 @@ impl Field {
EMPTY_VALUE => {
// #[patch(empty_value = ...)]
if empty_value.is_some() {
return Err(meta
.error("The empty value is already set, we can't defined more than once"));
return Err(meta.error(
"The empty value is already set, we can't defined more than once",
));
}
if let Some(lit) = crate::get_lit(path, &meta)? {
empty_value= Some(lit);
empty_value = Some(lit);
} else {
return Err(meta
.error("empty_value needs a clear value to define what is empty"));
Expand Down
12 changes: 6 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 23 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,28 @@
'';
updateDependencyScript = pkgs.writeShellScriptBin "update-dependency" ''
dr ./Cargo.toml
if [ -f "Cargo.toml.old" ]; then
rm Cargo.toml.old

cd no-std-examples
dr ./Cargo.toml

if [[ -f "Cargo.toml.old" || -f "no-std-examples/Cargo.toml.old" ]]; then
rm -f Cargo.toml.old
rm -f no-std-examples/Cargo.toml.old
exit 1
fi
'';
in
with pkgs;
{
devShells = {
devShells = let
noStdRust = rust-bin.stable.latest.default.override {
targets = [
"thumbv7m-none-eabi"
];
extensions = [ "rust-src" "llvm-tools-preview" ];
};
in
{
default = mkShell {
buildInputs = [
rust-bin.stable.latest.minimal
Expand All @@ -52,6 +65,13 @@
updateDependencyScript
];
};

no-std = mkShell {
buildInputs = [
noStdRust
qemu
];
};
};
}
);
Expand Down
4 changes: 3 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ merge = [
"struct-patch-derive/merge"
]

alloc = []
std = ["box", "option"]
box = []
box = ["alloc"]
option = []
nesting = [
"struct-patch-derive/nesting"
]
none_as_default = ["option"]
keep_none = ["option"]

61 changes: 61 additions & 0 deletions lib/src/box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![cfg(feature = "box")]
use crate::Patch;

extern crate alloc;
use alloc::boxed::Box;

impl<T, P> Patch<Box<P>> for T
where
T: Patch<P>,
{
fn apply(&mut self, patch: Box<P>) {
self.apply(*patch);
}

fn into_patch(self) -> Box<P> {
Box::new(self.into_patch())
}

fn into_patch_by_diff(self, previous_struct: Self) -> Box<P> {
Box::new(self.into_patch_by_diff(previous_struct))
}

fn new_empty_patch() -> Box<P> {
Box::new(T::new_empty_patch())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate as struct_patch;
use crate::Patch;
use alloc::string::String;

#[test]
fn test_patch_box_simple() {
#[derive(Patch, Debug, PartialEq)]
struct Item {
field: u32,
other: String,
}

let mut item = Item {
field: 1,
other: String::from("hello"),
};
let patch = Box::new(ItemPatch {
field: None,
other: Some(String::from("bye")),
});

item.apply(patch);
assert_eq!(
item,
Item {
field: 1,
other: String::from("bye")
}
);
}
}
13 changes: 9 additions & 4 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,24 @@
//! item.apply(filler_2);
//! assert_eq!(item.maybe_field_int, Some(7));
//! ```
#![cfg_attr(not(any(test, feature = "box", feature = "option")), no_std)]
#![no_std]

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

#[doc(hidden)]
pub use struct_patch_derive::Filler;
#[doc(hidden)]
pub use struct_patch_derive::Patch;
#[cfg(any(feature = "box", feature = "option"))]
pub mod std;
pub mod r#box;
pub mod option;
pub mod traits;
pub use traits::*;

#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::string::String;
use serde::Deserialize;
#[cfg(feature = "merge")]
use struct_patch::Merge;
Expand Down Expand Up @@ -486,7 +491,7 @@ mod tests {
b: B,
}

let patches = vec![
let patches = alloc::vec![
APatch {
a: Some(1),
b: Some(BPatch {
Expand Down
Loading