Skip to content

Commit

Permalink
Split crate into a proc-macro and a regular library crate (#214)
Browse files Browse the repository at this point in the history
proc-macro crates are loaded by the compiler instead of by the crate
that depends on it. This means it's not possible to use anything else
than exported anything else than exported proc-macros. But in some cases
we want the generated derives to use structs or traits that are provided
by the library. Two examples of this are:
1. An error type that can be used by the `Into` derive ([#173])
2. Or to support trait objects for the `source` of the `Error` derive
([#122])

This change splits the layout of this crate in two. One crate that only
exports proc-macros and is loaded by the compiler. And a second crate
that re-exports all these macros but is also able to export different
things such as structs and traits.

NOTE: This only refactors the code in this repo. No functional changes
happen in this change. These will be done in follow-up PRs.

[#173]: JelteF/derive_more#173
[#122]: JelteF/derive_more#122

Required for #173 #122

Co-authored-by: tyranron <tyranron@gmail.com>
  • Loading branch information
liveseed and tyranron committed Nov 14, 2022
1 parent 586d97d commit 713ff36
Show file tree
Hide file tree
Showing 58 changed files with 370 additions and 284 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
toolchain: stable
components: clippy

- run: cargo clippy --all-features -- -D warnings
- run: cargo clippy --workspace --all-features -- -D warnings

rustfmt:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
- name: Install minimal dependencies versions
run: cargo +nightly update -Z minimal-versions

- run: cargo test --features testing-helpers
- run: cargo test --workspace --features testing-helpers

test:
strategy:
Expand All @@ -94,7 +94,7 @@ jobs:
toolchain: ${{ matrix.toolchain }}
- run: rustup default ${{ matrix.toolchain }}

- run: cargo test --features testing-helpers
- run: cargo test --workspace --features testing-helpers

test-features:
name: test features
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
with:
toolchain: nightly

- run: cargo +nightly doc
- run: cargo +nightly doc --workspace
env:
RUSTDOCFLAGS: --cfg docsrs

Expand All @@ -156,17 +156,17 @@ jobs:

- name: Parse release version
id: release
run: echo ::set-output
name=version::${GITHUB_REF#refs/tags/v}
run: echo "version=${GITHUB_REF#refs/tags/v}"
>> $GITHUB_OUTPUT
- name: Verify release version matches `derive_more` Cargo manifest
run: |
test "${{ steps.release.outputs.version }}" \
== "$(grep -m1 'version = "' Cargo.toml | cut -d '"' -f2)"
- name: Parse CHANGELOG link
id: changelog
run: echo ::set-output
name=link::${{ github.server_url }}/${{ github.repository }}/blob/v${{ steps.release.outputs.version }}/CHANGELOG.md#$(sed -n '/^## ${{ steps.release.outputs.version }}/{s/^## \([^ ]*\) - \([0-9].*\)/\1---\2/;s/[^0-9a-z-]*//g;p;}' CHANGELOG.md)
run: echo "link=${{ github.server_url }}/${{ github.repository }}/blob/v${{ steps.release.outputs.version }}/CHANGELOG.md#$(sed -n '/^## ${{ steps.release.outputs.version }}/{s/^## \([^ ]*\) - \([0-9].*\)/\1---\2/;s/[^0-9a-z-]*//g;p;}' CHANGELOG.md)"
>> $GITHUB_OUTPUT

- uses: softprops/action-gh-release@v1
with:
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ If a new behaviour makes sense, that is different from the current behavior, the

### Documentation

Documentation is contained in the `doc/*.md` files and [README].
Documentation is contained in the `impl/doc/*.md` files and [README].

Documentation should be up-to-date with any [PR] changes visible for library end-users.

Expand All @@ -51,7 +51,7 @@ The best strategy for writing a new integration test is to look at existing inte

#### Documentation tests

These are the [code examples][1] in the `doc/*.md` files and [README].
These are the [code examples][1] in the `impl/doc/*.md` files and [README].

Writing documentation tests is needed for better illustration of the added/altered capabilities for end-users of the crate.

Expand Down Expand Up @@ -92,7 +92,7 @@ To produce a new release of the `derive_more` crate, perform the following steps

1. Complete the existing [CHANGELOG] or fill up a new one for the new version.
2. Update [README] installation instructions with the new version.
3. Run `cargo release patch` (or `minor`/`major`).
3. Run `cargo release patch --workspace` (or `minor`/`major`).
4. Wait for the CI pipeline to complete successfully, and the [GitHub release] being created.


Expand Down
64 changes: 28 additions & 36 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,18 @@ categories = ["development-tools", "development-tools::procedural-macro-helpers"

include = [
"src/**/*.rs",
"doc/**/*.md",
"Cargo.toml",
"LICENSE",
"README.md",
"CHANGELOG.md",
"tests/**/*.rs", # debian packaging wants this
]

[lib]
name = "derive_more"
proc-macro = true
[workspace]
members = ["impl"]

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.81"
convert_case = { version = "0.6", optional = true }
unicode-xid = { version = "0.2.2", optional = true }

[build-dependencies]
rustc_version = { version = "0.4", optional = true }
derive_more-impl = { version = "=0.99.17", path = "impl" }

[dev-dependencies]
rustversion = "1.0"
Expand All @@ -48,30 +39,29 @@ github = { repository = "JelteF/derive_more", workflow = "CI" }
rustdoc-args = ["--cfg", "docsrs"]

[features]
add_assign = []
add = []
as_mut = []
as_ref = []
constructor = []
deref = []
deref_mut = []
display = ["syn/extra-traits", "unicode-xid"]
error = ["syn/extra-traits"]
from = ["syn/extra-traits"]
from_str = ["convert_case"]
index = []
index_mut = []
into = ["syn/extra-traits"]
into_iterator = []
iterator = []
mul_assign = ["syn/extra-traits"]
mul = ["syn/extra-traits"]
not = ["syn/extra-traits"]
sum = []
try_into = ["syn/extra-traits"]
testing-helpers = ["rustc_version"]
is_variant = ["convert_case"]
unwrap = ["convert_case", "rustc_version"]
add_assign = ["derive_more-impl/add_assign"]
add = ["derive_more-impl/add"]
as_mut = ["derive_more-impl/as_mut"]
as_ref = ["derive_more-impl/as_ref"]
constructor = ["derive_more-impl/constructor"]
deref = ["derive_more-impl/deref"]
deref_mut = ["derive_more-impl/deref_mut"]
display = ["derive_more-impl/display"]
error = ["derive_more-impl/error"]
from = ["derive_more-impl/from"]
from_str = ["derive_more-impl/from_str"]
index = ["derive_more-impl/index"]
index_mut = ["derive_more-impl/index_mut"]
into = ["derive_more-impl/into"]
into_iterator = ["derive_more-impl/into_iterator"]
iterator = ["derive_more-impl/iterator"]
mul_assign = ["derive_more-impl/mul_assign"]
mul = ["derive_more-impl/mul"]
not = ["derive_more-impl/not"]
sum = ["derive_more-impl/sum"]
try_into = ["derive_more-impl/try_into"]
is_variant = ["derive_more-impl/is_variant"]
unwrap = ["derive_more-impl/unwrap"]

default = [
"add_assign",
Expand Down Expand Up @@ -99,6 +89,8 @@ default = [
"unwrap"
]

testing-helpers = ["derive_more-impl/testing-helpers"]

[[test]]
name = "add_assign"
path = "tests/add_assign.rs"
Expand Down
2 changes: 1 addition & 1 deletion ci/test_all_features.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
set -euxo pipefail

for feature in $(tomljson Cargo.toml | jq --raw-output '.features | keys[]' | grep -v 'default\|testing-helpers'); do
cargo test --tests --no-default-features --features "$feature,testing-helpers";
cargo test -p derive_more --tests --no-default-features --features "$feature,testing-helpers";
done
70 changes: 70 additions & 0 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
[package]
name = "derive_more-impl"
version = "0.99.17" # should be the same as main crate version
edition = "2021"
rust-version = "1.56.0"
description = "Internal implementation of `derive_more` crate"
authors = ["Jelte Fennema <github-tech@jeltef.nl>"]
license = "MIT"
repository = "https://github.com/JelteF/derive_more"
documentation = "https://docs.rs/derive_more"

# explicitly no keywords or categories so it cannot be found easily

include = [
"src/**/*.rs",
"doc/**/*.md",
"Cargo.toml",
"LICENSE",
]

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.81"
convert_case = { version = "0.6", optional = true }
unicode-xid = { version = "0.2.2", optional = true }

[build-dependencies]
rustc_version = { version = "0.4", optional = true }

[dev-dependencies]
derive_more = { path = ".." }

[badges]
github = { repository = "JelteF/derive_more", workflow = "CI" }

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]

[features]
add_assign = []
add = []
as_mut = []
as_ref = []
constructor = []
deref = []
deref_mut = []
display = ["syn/extra-traits", "unicode-xid"]
error = ["syn/extra-traits"]
from = ["syn/extra-traits"]
from_str = ["convert_case"]
index = []
index_mut = []
into = ["syn/extra-traits"]
into_iterator = []
iterator = []
mul_assign = ["syn/extra-traits"]
mul = ["syn/extra-traits"]
not = ["syn/extra-traits"]
sum = []
try_into = ["syn/extra-traits"]
is_variant = ["convert_case"]
unwrap = ["convert_case"]

default = []

testing-helpers = ["rustc_version"]
1 change: 1 addition & 0 deletions impl/LICENSE
12 changes: 12 additions & 0 deletions impl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `derive_more-impl`

This crate is an implementation detail of the [`derive_more`][crates.io]. If you
found this crate by accident you're probably looking for one of the following
pages of [`derive_more`][crates.io]:
1. [crates.io]
2. [docs.rs]
3. [GitHub]

[crates.io]: https://crates.io/crates/derive_more
[docs.rs]: https://docs.rs/derive_more/latest/derive_more
[GitHub]: https://github.com/JelteF/derive_more
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions doc/error.md → impl/doc/error.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ ignored for one of these methods by using `#[error(not(backtrace))]` or
## Example usage

```rust
# #![cfg_attr(feature = "nightly", feature(error_generic_member_access, provide_any))]
# #![cfg_attr(nightly, feature(error_generic_member_access, provide_any))]
// Nightly requires enabling this features:
// #![feature(error_generic_member_access, provide_any)]
# #[cfg(not(feature = "nightly"))] fn main() {}
# #[cfg(feature = "nightly")] fn main() {
# #[cfg(not(nightly))] fn main() {}
# #[cfg(nightly)] fn main() {
# use std::{any, error::Error as _, backtrace::Backtrace};
#
# use derive_more::{Display, Error, From};
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 713ff36

Please sign in to comment.