Skip to content

Commit

Permalink
Merge pull request #25 from kas-gui/work
Browse files Browse the repository at this point in the history
0.5.2: add singleton! macro
  • Loading branch information
dhardy authored Oct 6, 2022
2 parents d5e3e9c + 7703c4a commit fe651f1
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.5.2] — 2022-10-06

- Add `singleton!` macro (#25)

## [0.5.1] — 2022-09-23

- Fix: do not copy attributes on trait items (#24)
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "impl-tools"
version = "0.5.1"
version = "0.5.2"
authors = ["Diggory Hardy <git@dhardy.name>"]
edition = "2021"
license = "MIT/Apache-2.0"
Expand All @@ -21,7 +21,7 @@ proc-macro-error = "1.0"
version = "1.0.14"

[dependencies.impl-tools-lib]
version = "0.5.1"
version = "0.5.2"
path = "lib"

[dev-dependencies]
Expand Down
35 changes: 28 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ fn main() {
`#[impl_default]` implements `std::default::Default`:

```rust
use impl_tools::{impl_default, impl_scope};

#[impl_default(Tree::Ash)]
#[impl_tools::impl_default(Tree::Ash)]
enum Tree { Ash, Beech, Birch, Willow }

impl_scope! {
impl_tools::impl_scope! {
#[impl_default]
struct Copse {
tree_type: Tree,
Expand All @@ -85,16 +83,17 @@ impl_scope! {
}
```

Note: `#[impl_default]` is matched within an `impl_scope!` regardless of imports.

### Impl Scope

`impl_scope!` is a function-like macro used to define a type plus its
implementations. It supports `impl Self` syntax:

```rust
use impl_tools::impl_scope;
use std::fmt::Display;

impl_scope! {
impl_tools::impl_scope! {
/// I don't know why this exists
pub struct NamedThing<T: Display, F> {
name: T,
Expand All @@ -118,7 +117,29 @@ impl_scope! {
```

Caveat: `rustfmt` won't currently touch the contents. Hopefully that
[can be fixed](https://github.com/rust-lang/rustfmt/issues/5254)!
[can be fixed](https://github.com/rust-lang/rustfmt/pull/5538)!

### Singleton

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

Example:
```rust
use std::fmt;
fn main() {
let world = "world";
let says_hello_world = impl_tools::singleton! {
struct(&'static str = world);
impl fmt::Display for Self {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "hello {}", self.0)
}
}
};
assert_eq!(format!("{}", says_hello_world), "hello world");
}
```


Extensibility
Expand Down
4 changes: 2 additions & 2 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "impl-tools-lib"
version = "0.5.1"
version = "0.5.2"
authors = ["Diggory Hardy <git@dhardy.name>"]
edition = "2021"
license = "MIT/Apache-2.0"
Expand All @@ -19,4 +19,4 @@ proc-macro-error = "1.0"
version = "1.0.14"
# We need 'extra-traits' for equality testing
# We need 'full' for parsing macros within macro arguments
features = ["extra-traits", "full"]
features = ["extra-traits", "full", "visit", "visit-mut"]
10 changes: 6 additions & 4 deletions lib/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Impl-tools library
=======

This is the library behind the [impl-tools](https://crates.io/crates/impl-tools) crate.
This is the library behind the [impl-tools] crate.

The only reason to use this library directly is to extend the impl-tools macros
to support custom traits in `#[autoimpl]` or custom attributes evaluated from
`impl_scope!`.
This library may be used directly to write custom variants of the [impl-tools]
macros, for example extending `#[autoimpl]` to support other traits or
writing new attribute macros to be evaluated within a `impl_scope!`.

[impl-tools]: https://crates.io/crates/impl-tools


Copyright and Licence
Expand Down
10 changes: 10 additions & 0 deletions lib/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,13 @@ impl Parse for ImplDefault {
})
}
}

/// Helper fn which can be passed to [`Scope::apply_attrs`]
///
/// This optionally matches [`AttrImplDefault`].
pub fn find_attr_impl_default(path: &syn::Path) -> Option<&'static dyn ScopeAttr> {
AttrImplDefault
.path()
.matches(path)
.then(|| &AttrImplDefault as &dyn ScopeAttr)
}
11 changes: 11 additions & 0 deletions lib/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ use syn::parse::{Parse, ParseStream, Result};
use syn::punctuated::Punctuated;
use syn::{token, Attribute, Expr, Ident, Token, Type, Visibility};

/// Struct style: unit/tuple/regular
#[derive(Debug)]
pub enum StructStyle {
/// A unit struct (e.g. `struct Foo;`)
Unit(Token![;]),
/// A tuple struct (e.g. `struct Foo(f32, f32);`)
Tuple(token::Paren, Token![;]),
/// A regular struct (e.g. `struct Foo { x: f32, y: f32 }`)
Regular(token::Brace),
}

/// Data stored within an enum variant or struct.
///
/// This is a variant of [`syn::Fields`] supporting field initializers.
Expand Down
4 changes: 3 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ pub mod fields;
mod for_deref;
pub mod generics;
mod scope;
mod singleton;

pub use default::{AttrImplDefault, ImplDefault};
pub use default::{find_attr_impl_default, AttrImplDefault, ImplDefault};
pub use for_deref::ForDeref;
pub use scope::{Scope, ScopeAttr, ScopeItem};
pub use singleton::{Singleton, SingletonField, SingletonScope};

/// Simple, allocation-free path representation
#[derive(PartialEq, Eq)]
Expand Down
Loading

0 comments on commit fe651f1

Please sign in to comment.