Skip to content

Commit fdf30dc

Browse files
Relax derive for structs by only requiring Atom field
Before, `PrimitiveAtom` was required. But `Atom` is absolutely enough.
1 parent 439592b commit fdf30dc

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

atomig-macro/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn atom_impl_for_struct(s: &DataStruct) -> Result<TokenStream2, Error> {
105105
return Err(Error::new(s.fields.span(), msg));
106106
}
107107

108-
// Generate the code for `pack` and `unpack` which depends on weather it is
108+
// Generate the code for `pack` and `unpack` which depends on whether it is
109109
// a named or tuple-struct field.
110110
let (field_access, struct_init) = match &field.ident {
111111
Some(name) => (quote! { self.#name }, quote! { Self { #name: src } }),
@@ -114,12 +114,13 @@ fn atom_impl_for_struct(s: &DataStruct) -> Result<TokenStream2, Error> {
114114

115115
let field_type = &field.ty;
116116
Ok(quote! {
117-
type Repr = #field_type;
117+
type Repr = <#field_type as atomig::Atom>::Repr;
118118

119119
fn pack(self) -> Self::Repr {
120-
#field_access
120+
<#field_type as atomig::Atom>::pack(#field_access)
121121
}
122122
fn unpack(src: Self::Repr) -> Self {
123+
let src = <#field_type as atomig::Atom>::unpack(src);
123124
#struct_init
124125
}
125126
})

examples/custom_type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! deriving `Atom`.
33
44
use std::{
5+
num::NonZeroU16,
56
sync::Arc,
67
thread,
78
time::Duration,
@@ -20,6 +21,9 @@ enum Animal {
2021
#[derive(Debug, PartialEq, Atom, AtomLogic)]
2122
struct BitSet(u16);
2223

24+
// The inner type only has to implement `Atom`, not `PrimitiveAtom`.
25+
#[derive(Debug, PartialEq, Atom)]
26+
struct Port(NonZeroU16);
2327

2428
fn main() {
2529
// Example with `Animal`

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ pub use atomig_macro::{Atom, AtomInteger, AtomLogic};
163163
///
164164
/// The trait can be automatically derived for two kinds of types:
165165
/// - `struct` types with only *one* field. That field's type has to implement
166-
/// `PrimitiveAtom` and is used as `Repr` type. Works with tuple structs or
167-
/// normal structs with named fields.
166+
/// `Atom`. Works with tuple structs or normal structs with one named field.
168167
/// - `enum` types that have a `#[repr(_)]` attribute specified and are C-like
169168
/// (i.e. no variant has any fields). The primitive type specified in the
170169
/// `#[repr(_)]` attribute is used as `Repr` type.

0 commit comments

Comments
 (0)