Skip to content

Commit 3fe5057

Browse files
authored
Merge pull request #36 from kas-gui/work
v0.10: rename singleton! → impl_anon!
2 parents 73c70c2 + 73c5512 commit 3fe5057

15 files changed

Lines changed: 153 additions & 56 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
33
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
44

5+
## [0.10.0] — 2023-09-07
6+
7+
- Rename `singleton!``impl_anon!` (#36)
8+
- Reorganise `impl-tools-lib`: new `anon` and `scope` public modules (#36)
9+
510
## [0.9.1] — 2023-09-07
611

712
- Fix clone for fields which auto-deref (issue #34)

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "impl-tools"
3-
version = "0.9.1"
3+
version = "0.10.0"
44
authors = ["Diggory Hardy <git@dhardy.name>"]
55
edition = "2021"
66
license = "MIT/Apache-2.0"
@@ -22,7 +22,7 @@ default-features = false
2222
version = "2.0.0"
2323

2424
[dependencies.impl-tools-lib]
25-
version = "0.9.1"
25+
version = "0.10.0"
2626
path = "lib"
2727

2828
[dev-dependencies]

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,17 @@ impl_tools::impl_scope! {
159159
Caveat: `rustfmt` won't currently touch the contents. Hopefully that
160160
[can be fixed](https://github.com/rust-lang/rustfmt/pull/5538)!
161161

162-
### Singleton
162+
### Impl Anon
163163

164-
`singleton!` is a function-like macro to construct a single-use struct with
164+
`impl_anon!` is a function-like macro to construct a single-use struct with
165165
custom implementations (similar: [RFC#2604](https://github.com/rust-lang/rfcs/pull/2604)).
166166

167167
Example:
168168
```rust
169169
use std::fmt;
170170
fn main() {
171171
let world = "world";
172-
let says_hello_world = impl_tools::singleton! {
172+
let says_hello_world = impl_tools::impl_anon! {
173173
struct(&'static str = world);
174174
impl fmt::Display for Self {
175175
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "impl-tools-lib"
3-
version = "0.9.1"
3+
version = "0.10.0"
44
authors = ["Diggory Hardy <git@dhardy.name>"]
55
edition = "2021"
66
license = "MIT/Apache-2.0"
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
44
// https://www.apache.org/licenses/LICENSE-2.0
55

6+
//! The `impl_anon!` macro
7+
68
use crate::fields::{Field, Fields, FieldsNamed, FieldsUnnamed, StructStyle};
7-
use crate::{IdentFormatter, Scope, ScopeItem};
9+
use crate::scope::{Scope, ScopeItem};
10+
use crate::IdentFormatter;
811
use proc_macro2::{Span, TokenStream};
912
use quote::{quote, ToTokens, TokenStreamExt};
1013
use syn::token::{Brace, Colon, Comma, Eq, Paren, Semi};
1114
use syn::{parse_quote, punctuated::Punctuated, spanned::Spanned};
1215
use syn::{Attribute, GenericParam, Generics, Ident, ItemImpl, Member, Token, Type, TypePath};
1316

14-
/// A field of a [`Singleton`]
17+
/// A field of a [`Anon`]
1518
#[derive(Debug)]
16-
pub struct SingletonField {
19+
pub struct AnonField {
1720
/// Field attributes
1821
pub attrs: Vec<Attribute>,
1922
/// Field visibility
@@ -32,13 +35,13 @@ pub struct SingletonField {
3235
pub assignment: Option<(Eq, syn::Expr)>,
3336
}
3437

35-
/// A struct with a single instantiation
38+
/// Contents of `impl_anon!`
3639
///
37-
/// The singleton macro may be used to conveniently declare a struct's type,
40+
/// The impl_anon macro may be used to conveniently declare a struct's type,
3841
/// implementations and construct an instance.
3942
/// This struct represents the macro's input.
4043
#[derive(Debug)]
41-
pub struct Singleton {
44+
pub struct Anon {
4245
/// Struct attributes
4346
pub attrs: Vec<Attribute>,
4447
/// `struct` token
@@ -50,14 +53,14 @@ pub struct Singleton {
5053
/// Struct style: unit/tuple/regular
5154
pub style: StructStyle,
5255
/// Struct fields
53-
pub fields: Punctuated<SingletonField, Comma>,
56+
pub fields: Punctuated<AnonField, Comma>,
5457
/// (Explicit) struct implementations
5558
pub impls: Vec<ItemImpl>,
5659
}
5760

58-
impl Singleton {
59-
/// Convert to a [`SingletonScope`]
60-
pub fn into_scope(mut self) -> SingletonScope {
61+
impl Anon {
62+
/// Convert to a [`AnonScope`]
63+
pub fn into_scope(mut self) -> AnonScope {
6164
let mut idfmt = IdentFormatter::new();
6265

6366
let mut fields = Punctuated::<Field, Comma>::new();
@@ -221,7 +224,7 @@ impl Singleton {
221224
let scope = Scope {
222225
attrs: self.attrs,
223226
vis: syn::Visibility::Inherited,
224-
ident: parse_quote! { _Singleton },
227+
ident: parse_quote! { _Anon },
225228
generics: self.generics,
226229
item: ScopeItem::Struct {
227230
token: self.token,
@@ -232,7 +235,7 @@ impl Singleton {
232235
generated: vec![],
233236
};
234237

235-
SingletonScope(scope, field_val_toks)
238+
AnonScope(scope, field_val_toks)
236239
}
237240
}
238241

@@ -243,21 +246,21 @@ impl Singleton {
243246
///
244247
/// Tokens may be generated by [`Self::expand`].
245248
#[derive(Debug)]
246-
pub struct SingletonScope(Scope, TokenStream);
249+
pub struct AnonScope(Scope, TokenStream);
247250

248-
impl std::ops::Deref for SingletonScope {
251+
impl std::ops::Deref for AnonScope {
249252
type Target = Scope;
250253
fn deref(&self) -> &Scope {
251254
&self.0
252255
}
253256
}
254-
impl std::ops::DerefMut for SingletonScope {
257+
impl std::ops::DerefMut for AnonScope {
255258
fn deref_mut(&mut self) -> &mut Scope {
256259
&mut self.0
257260
}
258261
}
259262

260-
impl SingletonScope {
263+
impl AnonScope {
261264
/// Generate the [`TokenStream`]
262265
///
263266
/// This is a convenience function. It is valid to, instead, (1) call
@@ -269,7 +272,7 @@ impl SingletonScope {
269272
}
270273
}
271274

272-
impl ToTokens for SingletonScope {
275+
impl ToTokens for AnonScope {
273276
fn to_tokens(&self, tokens: &mut TokenStream) {
274277
let scope = &self.0;
275278
let field_val_toks = &self.1;
@@ -278,7 +281,7 @@ impl ToTokens for SingletonScope {
278281
{
279282
#scope
280283

281-
_Singleton {
284+
_Anon {
282285
#field_val_toks
283286
}
284287
}
@@ -389,7 +392,7 @@ mod parsing {
389392
})
390393
}
391394

392-
impl SingletonField {
395+
impl AnonField {
393396
fn check_is_fixed(ty: &Type, input_span: Span) -> Result<()> {
394397
let is_fixed = match ty {
395398
Type::ImplTrait(_) | Type::Infer(_) => false,
@@ -448,7 +451,7 @@ mod parsing {
448451
Self::check_is_fixed(&ty, input.span())?;
449452
}
450453

451-
Ok(SingletonField {
454+
Ok(AnonField {
452455
attrs,
453456
vis,
454457
ident,
@@ -471,7 +474,7 @@ mod parsing {
471474
Self::check_is_fixed(&ty, input.span())?;
472475
}
473476

474-
Ok(SingletonField {
477+
Ok(AnonField {
475478
attrs,
476479
vis,
477480
ident: None,
@@ -482,7 +485,7 @@ mod parsing {
482485
}
483486
}
484487

485-
impl Parse for Singleton {
488+
impl Parse for Anon {
486489
fn parse(input: ParseStream) -> Result<Self> {
487490
let attrs = input.call(Attribute::parse_outer)?;
488491
let token = input.parse::<Token![struct]>()?;
@@ -500,7 +503,7 @@ mod parsing {
500503
if generics.where_clause.is_none() && lookahead.peek(Paren) {
501504
let content;
502505
let paren_token = parenthesized!(content in input);
503-
fields = content.parse_terminated(SingletonField::parse_unnamed, Token![,])?;
506+
fields = content.parse_terminated(AnonField::parse_unnamed, Token![,])?;
504507

505508
lookahead = input.lookahead1();
506509
if lookahead.peek(Token![where]) {
@@ -517,7 +520,7 @@ mod parsing {
517520
let content;
518521
let brace_token = braced!(content in input);
519522
style = StructStyle::Regular(brace_token);
520-
fields = content.parse_terminated(SingletonField::parse_named, Token![,])?;
523+
fields = content.parse_terminated(AnonField::parse_named, Token![,])?;
521524
} else if lookahead.peek(Semi) {
522525
style = StructStyle::Unit(input.parse()?);
523526
fields = Punctuated::new();
@@ -530,7 +533,7 @@ mod parsing {
530533
impls.push(parse_impl(None, input)?);
531534
}
532535

533-
Ok(Singleton {
536+
Ok(Anon {
534537
attrs,
535538
token,
536539
generics,

lib/src/autoimpl.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//! Implementation of the `#[autoimpl]` attribute
77
88
use crate::generics::{clause_to_toks, WhereClause};
9-
use crate::{ForDeref, SimplePath};
9+
use crate::SimplePath;
1010
use proc_macro2::{Span, TokenStream as Toks};
1111
use proc_macro_error::emit_error;
1212
use quote::{quote, TokenStreamExt};
@@ -17,9 +17,11 @@ use syn::{
1717
Token,
1818
};
1919

20+
mod for_deref;
2021
mod impl_misc;
2122
mod impl_using;
2223

24+
pub use for_deref::ForDeref;
2325
pub use impl_misc::*;
2426
pub use impl_using::*;
2527

lib/src/default.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
use crate::fields::{Fields, FieldsNamed, FieldsUnnamed};
77
use crate::generics::{clause_to_toks, WhereClause};
8-
use crate::{Scope, ScopeAttr, ScopeItem, SimplePath};
8+
use crate::scope::{Scope, ScopeAttr, ScopeItem};
9+
use crate::SimplePath;
910
use proc_macro2::{Span, TokenStream};
1011
use proc_macro_error::emit_error;
1112
use quote::quote;
@@ -189,7 +190,7 @@ impl Parse for ImplDefault {
189190
/// Helper fn which can be passed to [`Scope::apply_attrs`]
190191
///
191192
/// This optionally matches [`AttrImplDefault`].
192-
pub fn find_attr_impl_default(path: &syn::Path) -> Option<&'static dyn ScopeAttr> {
193+
pub fn find_impl_default(path: &syn::Path) -> Option<&'static dyn ScopeAttr> {
193194
AttrImplDefault
194195
.path()
195196
.matches(path)

lib/src/fields.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pub struct Field {
7676
/// Optional field initializer.
7777
///
7878
/// This is considered legal input when parsing, but not legal output. An
79-
/// attribute rule such as [`crate::AttrImplDefault`] must remove the
80-
/// initializer before output is generated.
79+
/// attribute rule such as [`AttrImplDefault`](crate::scope::AttrImplDefault)
80+
/// must remove the initializer before output is generated.
8181
pub assign: Option<(Token![=], Expr)>,
8282
}
8383

lib/src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,26 @@
1414
#![allow(clippy::unnecessary_lazy_evaluations)]
1515
#![allow(clippy::style)]
1616

17+
pub mod anon;
1718
pub mod autoimpl;
1819
mod default;
1920
pub mod fields;
20-
mod for_deref;
2121
pub mod generics;
22-
mod scope;
23-
mod singleton;
22+
pub mod scope;
2423

25-
pub use default::{find_attr_impl_default, AttrImplDefault, ImplDefault};
26-
pub use for_deref::ForDeref;
24+
pub use default::ImplDefault;
2725
use proc_macro2::Span;
28-
pub use scope::{Scope, ScopeAttr, ScopeItem};
29-
pub use singleton::{Singleton, SingletonField, SingletonScope};
3026
use syn::Ident;
3127

32-
/// Tool to make a formatted [`Ident`]
28+
/// Tool to make a formatted [`Ident`](struct@Ident)
3329
pub struct IdentFormatter(String);
3430
impl IdentFormatter {
3531
/// Construct a formatter
3632
pub fn new() -> Self {
3733
IdentFormatter(String::with_capacity(32))
3834
}
3935

40-
/// Construct a new [`Ident`]
36+
/// Construct a new [`Ident`](struct@Ident)
4137
pub fn make(&mut self, args: std::fmt::Arguments, span: Span) -> Ident {
4238
use std::fmt::Write;
4339

@@ -46,7 +42,7 @@ impl IdentFormatter {
4642
Ident::new(&self.0, span)
4743
}
4844

49-
/// Construct a new [`Ident`], using [`Span::call_site`]
45+
/// Construct a new [`Ident`](struct@Ident), using [`Span::call_site`]
5046
///
5147
/// # Example
5248
///

0 commit comments

Comments
 (0)