Skip to content

Commit a43676a

Browse files
committed
Rename singleton! -> impl_anon!
Singleton usually refers to a type with a single, static instance. The correct name is Anonymous Struct; impl_anon could possibly support other anonymous types in the future.
1 parent 32200cd commit a43676a

5 files changed

Lines changed: 40 additions & 40 deletions

File tree

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 {
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use syn::token::{Brace, Colon, Comma, Eq, Paren, Semi};
1111
use syn::{parse_quote, punctuated::Punctuated, spanned::Spanned};
1212
use syn::{Attribute, GenericParam, Generics, Ident, ItemImpl, Member, Token, Type, TypePath};
1313

14-
/// A field of a [`Singleton`]
14+
/// A field of a [`Anon`]
1515
#[derive(Debug)]
16-
pub struct SingletonField {
16+
pub struct AnonField {
1717
/// Field attributes
1818
pub attrs: Vec<Attribute>,
1919
/// Field visibility
@@ -32,13 +32,13 @@ pub struct SingletonField {
3232
pub assignment: Option<(Eq, syn::Expr)>,
3333
}
3434

35-
/// A struct with a single instantiation
35+
/// Contents of `impl_anon!`
3636
///
37-
/// The singleton macro may be used to conveniently declare a struct's type,
37+
/// The impl_anon macro may be used to conveniently declare a struct's type,
3838
/// implementations and construct an instance.
3939
/// This struct represents the macro's input.
4040
#[derive(Debug)]
41-
pub struct Singleton {
41+
pub struct Anon {
4242
/// Struct attributes
4343
pub attrs: Vec<Attribute>,
4444
/// `struct` token
@@ -50,14 +50,14 @@ pub struct Singleton {
5050
/// Struct style: unit/tuple/regular
5151
pub style: StructStyle,
5252
/// Struct fields
53-
pub fields: Punctuated<SingletonField, Comma>,
53+
pub fields: Punctuated<AnonField, Comma>,
5454
/// (Explicit) struct implementations
5555
pub impls: Vec<ItemImpl>,
5656
}
5757

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

6363
let mut fields = Punctuated::<Field, Comma>::new();
@@ -221,7 +221,7 @@ impl Singleton {
221221
let scope = Scope {
222222
attrs: self.attrs,
223223
vis: syn::Visibility::Inherited,
224-
ident: parse_quote! { _Singleton },
224+
ident: parse_quote! { _Anon },
225225
generics: self.generics,
226226
item: ScopeItem::Struct {
227227
token: self.token,
@@ -232,7 +232,7 @@ impl Singleton {
232232
generated: vec![],
233233
};
234234

235-
SingletonScope(scope, field_val_toks)
235+
AnonScope(scope, field_val_toks)
236236
}
237237
}
238238

@@ -243,21 +243,21 @@ impl Singleton {
243243
///
244244
/// Tokens may be generated by [`Self::expand`].
245245
#[derive(Debug)]
246-
pub struct SingletonScope(Scope, TokenStream);
246+
pub struct AnonScope(Scope, TokenStream);
247247

248-
impl std::ops::Deref for SingletonScope {
248+
impl std::ops::Deref for AnonScope {
249249
type Target = Scope;
250250
fn deref(&self) -> &Scope {
251251
&self.0
252252
}
253253
}
254-
impl std::ops::DerefMut for SingletonScope {
254+
impl std::ops::DerefMut for AnonScope {
255255
fn deref_mut(&mut self) -> &mut Scope {
256256
&mut self.0
257257
}
258258
}
259259

260-
impl SingletonScope {
260+
impl AnonScope {
261261
/// Generate the [`TokenStream`]
262262
///
263263
/// This is a convenience function. It is valid to, instead, (1) call
@@ -269,7 +269,7 @@ impl SingletonScope {
269269
}
270270
}
271271

272-
impl ToTokens for SingletonScope {
272+
impl ToTokens for AnonScope {
273273
fn to_tokens(&self, tokens: &mut TokenStream) {
274274
let scope = &self.0;
275275
let field_val_toks = &self.1;
@@ -278,7 +278,7 @@ impl ToTokens for SingletonScope {
278278
{
279279
#scope
280280

281-
_Singleton {
281+
_Anon {
282282
#field_val_toks
283283
}
284284
}
@@ -389,7 +389,7 @@ mod parsing {
389389
})
390390
}
391391

392-
impl SingletonField {
392+
impl AnonField {
393393
fn check_is_fixed(ty: &Type, input_span: Span) -> Result<()> {
394394
let is_fixed = match ty {
395395
Type::ImplTrait(_) | Type::Infer(_) => false,
@@ -448,7 +448,7 @@ mod parsing {
448448
Self::check_is_fixed(&ty, input.span())?;
449449
}
450450

451-
Ok(SingletonField {
451+
Ok(AnonField {
452452
attrs,
453453
vis,
454454
ident,
@@ -471,7 +471,7 @@ mod parsing {
471471
Self::check_is_fixed(&ty, input.span())?;
472472
}
473473

474-
Ok(SingletonField {
474+
Ok(AnonField {
475475
attrs,
476476
vis,
477477
ident: None,
@@ -482,7 +482,7 @@ mod parsing {
482482
}
483483
}
484484

485-
impl Parse for Singleton {
485+
impl Parse for Anon {
486486
fn parse(input: ParseStream) -> Result<Self> {
487487
let attrs = input.call(Attribute::parse_outer)?;
488488
let token = input.parse::<Token![struct]>()?;
@@ -500,7 +500,7 @@ mod parsing {
500500
if generics.where_clause.is_none() && lookahead.peek(Paren) {
501501
let content;
502502
let paren_token = parenthesized!(content in input);
503-
fields = content.parse_terminated(SingletonField::parse_unnamed, Token![,])?;
503+
fields = content.parse_terminated(AnonField::parse_unnamed, Token![,])?;
504504

505505
lookahead = input.lookahead1();
506506
if lookahead.peek(Token![where]) {
@@ -517,7 +517,7 @@ mod parsing {
517517
let content;
518518
let brace_token = braced!(content in input);
519519
style = StructStyle::Regular(brace_token);
520-
fields = content.parse_terminated(SingletonField::parse_named, Token![,])?;
520+
fields = content.parse_terminated(AnonField::parse_named, Token![,])?;
521521
} else if lookahead.peek(Semi) {
522522
style = StructStyle::Unit(input.parse()?);
523523
fields = Punctuated::new();
@@ -530,7 +530,7 @@ mod parsing {
530530
impls.push(parse_impl(None, input)?);
531531
}
532532

533-
Ok(Singleton {
533+
Ok(Anon {
534534
attrs,
535535
token,
536536
generics,

lib/src/lib.rs

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

17+
mod anon;
1718
pub mod autoimpl;
1819
mod default;
1920
pub mod fields;
2021
mod for_deref;
2122
pub mod generics;
2223
mod scope;
23-
mod singleton;
2424

25+
pub use anon::{Anon, AnonField, AnonScope};
2526
pub use default::{find_attr_impl_default, AttrImplDefault, ImplDefault};
2627
pub use for_deref::ForDeref;
2728
use proc_macro2::Span;
2829
pub use scope::{Scope, ScopeAttr, ScopeItem};
29-
pub use singleton::{Singleton, SingletonField, SingletonScope};
3030
use syn::Ident;
3131

3232
/// Tool to make a formatted [`Ident`]

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//! - Evaluation of advanced attribute macros, which may use field
2727
//! initializers and read/write other impls within the scope
2828
//!
29-
//! `singleton!` is a function-like macro used to define and instantiate a
29+
//! `impl_anon!` is a function-like macro used to define and instantiate a
3030
//! unique (single-use) type. It supports everything supported by `impl_scope!`
3131
//! plus field initializers and (limited) automatic typing of fields.
3232
//!
@@ -309,7 +309,7 @@ pub fn autoimpl(attr: TokenStream, item: TokenStream) -> TokenStream {
309309
toks
310310
}
311311

312-
/// Scope supporting `impl Self` and advanced attribute macros
312+
/// Implement a type with `impl Self` syntax
313313
///
314314
/// This macro facilitates definition of a type (struct, enum or union) plus
315315
/// implementations via `impl Self { .. }` syntax: `Self` is expanded to the
@@ -377,7 +377,7 @@ pub fn impl_scope(input: TokenStream) -> TokenStream {
377377
scope.expand().into()
378378
}
379379

380-
/// Construct a single-instance struct
380+
/// Construct an anonymous struct
381381
///
382382
/// Rust doesn't currently support [`impl Trait { ... }` expressions](https://github.com/canndrew/rfcs/blob/impl-trait-expressions/text/0000-impl-trait-expressions.md)
383383
/// or implicit typing of struct fields. This macro is a **hack** allowing that.
@@ -387,7 +387,7 @@ pub fn impl_scope(input: TokenStream) -> TokenStream {
387387
/// use std::fmt;
388388
/// fn main() {
389389
/// let world = "world";
390-
/// let says_hello_world = impl_tools::singleton! {
390+
/// let says_hello_world = impl_tools::impl_anon! {
391391
/// struct(&'static str = world);
392392
/// impl fmt::Display for Self {
393393
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -417,11 +417,11 @@ pub fn impl_scope(input: TokenStream) -> TokenStream {
417417
/// `impl Trait` type expressions. These are substituted with generics on the
418418
/// type.
419419
///
420-
/// Refer to [examples](https://github.com/search?q=singleton+repo%3Akas-gui%2Fkas+path%3Aexamples&type=Code) for usage.
420+
/// Refer to [examples](https://github.com/search?q=impl_anon+repo%3Akas-gui%2Fkas+path%3Aexamples&type=Code) for usage.
421421
#[proc_macro_error]
422422
#[proc_macro]
423-
pub fn singleton(input: TokenStream) -> TokenStream {
424-
let mut scope = parse_macro_input!(input as lib::Singleton).into_scope();
423+
pub fn impl_anon(input: TokenStream) -> TokenStream {
424+
let mut scope = parse_macro_input!(input as lib::Anon).into_scope();
425425
scope.apply_attrs(lib::find_attr_impl_default);
426426
scope.expand().into()
427427
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Test singleton!
1+
// Test impl_anon!
22

33
use core::fmt::Debug;
4-
use impl_tools::singleton;
4+
use impl_tools::impl_anon;
55

66
#[test]
77
fn a() {
8-
let a = singleton! {
8+
let a = impl_anon! {
99
#[derive(Clone, Debug)]
1010
struct<T: Clone + Debug>(T = "abc");
1111

@@ -21,7 +21,7 @@ fn a() {
2121

2222
#[test]
2323
fn b() {
24-
let b = singleton! {
24+
let b = impl_anon! {
2525
#[derive(Clone, Debug)]
2626
struct<T: Clone + Debug> {
2727
t: T = 123,

0 commit comments

Comments
 (0)