Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Fetch upstream changes #1

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
# Changelog

## Unreleased
## 0.24.3 (strum_macros)

* [#231](https://github.com/Peternator7/strum/pull/231) Add ignore lints for EnumIter not implementing Copy or Debug
on the generated iter struct. Copy should generally not be implemented on Iterators. Its an oversight that Debug isn't
implemented, but it will be a breaking change to add that so it'll be added in a future version.

## 0.24.2 (strum_macros)

* [#220](https://github.com/Peternator7/strum/pull/220). Add support for PHF in `EnumString` (opt-in runtime
performance improvements for large enums as `#[strum(use_phf)]`, requires `phf` feature and increases MSRV to `1.46`)
* [#224](https://github.com/Peternator7/strum/pull/224) tweaked the algorithm.

* Reverted [#217](https://github.com/peternator7/strum/pull/217) because it was disruptive and non-trivial to work around
if you hit it.

## ~~0.24.1~~ (Yanked becase #217 was more "breaking" than I wanted)

* [#220](https://github.com/Peternator7/strum/pull/220). Add support for PHF in `EnumString` (opt-in runtime
performance improvements for large enums as `#[strum(use_phf)]`, requires `phf` feature and increases MSRV to `1.46`)
* [#224](https://github.com/Peternator7/strum/pull/224) tweaked the algorithm.
* [#217](https://github.com/Peternator7/strum/pull/217): Automatically implement `TryFrom` in `FromRepr`. This is
technically a breaking change, but the fix is to just remove the manual implementation of TryFrom so it shouldn't
be more than a minor inconvenience.

## 0.24.0

Expand Down
21 changes: 13 additions & 8 deletions strum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strum"
version = "0.24.0"
name = "sea-strum"
version = "0.24.1"
edition = "2018"
authors = ["Peter Glotfelty <peter.glotfelty@microsoft.com>"]
license = "MIT"
Expand All @@ -9,25 +9,30 @@ description = "Helpful macros for working with enums and strings"
keywords = ["enum", "string", "macros", "proc-macros"]
categories = ["development-tools::procedural-macro-helpers", "parsing"]

documentation = "https://docs.rs/strum"
homepage = "https://github.com/Peternator7/strum"
repository = "https://github.com/Peternator7/strum"
documentation = "https://docs.rs/sea-strum"
homepage = "https://github.com/SeaQL/strum"
repository = "https://github.com/SeaQL/strum"
readme = "../README.md"

[lib]
name = "strum"
path = "src/lib.rs"

[dependencies]
strum_macros = { path = "../strum_macros", optional = true, version = "0.24" }
sea-strum_macros = { path = "../strum_macros", optional = true, version = "0.24" }
phf = { version = "0.10", features = ["macros"], optional = true }

[dev-dependencies]
strum_macros = { path = "../strum_macros", version = "0.24" }
sea-strum_macros = { path = "../strum_macros", version = "0.24" }

[badges]
travis-ci = { repository = "Peternator7/strum" }

[features]
default = ["std"]
derive = ["strum_macros"]
derive = ["sea-strum_macros"]
std = []
sea-orm = ["sea-strum_macros/sea-orm"]

[package.metadata.docs.rs]
features = ["derive"]
Expand Down
13 changes: 8 additions & 5 deletions strum_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "strum_macros"
version = "0.24.0"
name = "sea-strum_macros"
version = "0.24.3"
edition = "2018"
authors = ["Peter Glotfelty <peter.glotfelty@microsoft.com>"]
license = "MIT"
Expand All @@ -9,9 +9,9 @@ description = "Helpful macros for working with enums and strings"
keywords = ["enum", "string", "macros", "proc-macros"]
categories = ["development-tools::procedural-macro-helpers", "parsing"]

documentation = "https://docs.rs/strum"
homepage = "https://github.com/Peternator7/strum"
repository = "https://github.com/Peternator7/strum"
documentation = "https://docs.rs/sea-strum"
homepage = "https://github.com/SeaQL/strum"
repository = "https://github.com/SeaQL/strum"
readme = "../README.md"

[lib]
Expand All @@ -27,3 +27,6 @@ syn = { version = "1.0", features = ["parsing", "extra-traits"] }

[dev-dependencies]
strum = "0.24"

[features]
sea-orm = []
11 changes: 10 additions & 1 deletion strum_macros/src/helpers/type_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ impl StrumTypeProperties {
pub fn crate_module_path(&self) -> Path {
self.crate_module_path
.as_ref()
.map_or_else(|| parse_quote!(::strum), |path| parse_quote!(#path))
.map_or_else(|| {
#[cfg(feature = "sea-orm")]
{
parse_quote!(sea_orm::strum)
}
#[cfg(not(feature = "sea-orm"))]
{
parse_quote!(::strum)
}
}, |path| parse_quote!(#path))
}
}
3 changes: 1 addition & 2 deletions strum_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ pub fn enum_iter(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
toks.into()
}

/// Add a function to enum that allows accessing variants by its discriminant.
/// On Rust 1.34 and above, std::convert::TryFrom<TDiscriminant> will be derived as well.
/// Add a function to enum that allows accessing variants by its discriminant
///
/// This macro adds a standalone function to obtain an enum variant by its discriminant. The macro adds
/// `from_repr(discriminant: usize) -> Option<YourEnum>` as a standalone function on the enum. For
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/src/macros/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

Ok(quote! {
#[doc = "An iterator over the variants of [Self]"]
#[allow(
missing_copy_implementations,
missing_debug_implementations,
)]
#vis struct #iter_name #ty_generics {
idx: usize,
back_idx: usize,
Expand Down
53 changes: 2 additions & 51 deletions strum_macros/src/macros/from_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
use syn::{Data, DeriveInput, Fields, PathArguments, Type, TypeParen};

use crate::helpers::{non_enum_error, HasStrumVariantProperties, HasTypeProperties};
use crate::helpers::{non_enum_error, HasStrumVariantProperties};

pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let name = &ast.ident;
Expand All @@ -12,9 +12,6 @@ pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let vis = &ast.vis;
let attrs = &ast.attrs;

let type_properties = ast.get_type_properties()?;
let strum_module_path = type_properties.crate_module_path();

let mut discriminant_type: Type = syn::parse("usize".parse().unwrap()).unwrap();
for attr in attrs {
let path = &attr.path;
Expand Down Expand Up @@ -127,7 +124,7 @@ pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
filter_by_rust_version(quote! { const })
};

let from_repr = quote! {
Ok(quote! {
#[allow(clippy::use_self)]
impl #impl_generics #name #ty_generics #where_clause {
#[doc = "Try to create [Self] from the raw representation"]
Expand All @@ -138,51 +135,5 @@ pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}
}
}
};

let try_from_repr = try_from_repr(
name,
&discriminant_type,
&impl_generics,
&ty_generics,
where_clause,
&strum_module_path,
);

Ok(quote! {
#from_repr
#try_from_repr
})
}

#[rustversion::before(1.34)]
fn try_from_repr(
_name: &proc_macro2::Ident,
_discriminant_type: &Type,
_impl_generics: &syn::ImplGenerics,
_ty_generics: &syn::TypeGenerics,
_where_clause: Option<&syn::WhereClause>,
_strum_module_path: &syn::Path,
) -> TokenStream {
Default::default()
}

#[rustversion::since(1.34)]
fn try_from_repr(
name: &proc_macro2::Ident,
discriminant_type: &Type,
impl_generics: &syn::ImplGenerics,
ty_generics: &syn::TypeGenerics,
where_clause: Option<&syn::WhereClause>,
strum_module_path: &syn::Path,
) -> TokenStream {
quote! {
#[allow(clippy::use_self)]
impl #impl_generics ::core::convert::TryFrom<#discriminant_type> for #name #ty_generics #where_clause {
type Error = #strum_module_path::ParseError;
fn try_from(s: #discriminant_type) -> ::core::result::Result< #name #ty_generics , <Self as ::core::convert::TryFrom<#discriminant_type>>::Error> {
Self::from_repr(s).ok_or(#strum_module_path::ParseError::VariantNotFound)
}
}
}
}
4 changes: 2 additions & 2 deletions strum_nostd_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
strum = { path = "../strum", features = ["derive"] }
strum_macros = { path = "../strum_macros", features = [] }
sea-strum = { path = "../strum", features = ["derive"] }
sea-strum_macros = { path = "../strum_macros", features = [] }

[dev-dependencies]
rustversion = "1.0"
7 changes: 3 additions & 4 deletions strum_tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]

[features]
default = []
test_phf = ["strum/phf"]
test_phf = ["sea-strum/phf"]

[dependencies]
strum = { path = "../strum", features = ["derive"] }
strum_macros = { path = "../strum_macros", features = [] }
sea-strum = { path = "../strum", features = ["derive"] }
sea-strum_macros = { path = "../strum_macros", features = [] }
clap = "=2.33.0"
enum_variant_type = "=0.2.0"
structopt = "0.2.18"
bitflags = "=1.2"
if_rust_version = "1.0"

[dev-dependencies]
rustversion = "1.0"
36 changes: 9 additions & 27 deletions strum_tests/tests/from_repr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use if_rust_version::if_rust_version;
use strum::FromRepr;

#[derive(Debug, FromRepr, PartialEq)]
Expand All @@ -13,31 +12,14 @@ enum Week {
Saturday = 8,
}

macro_rules! assert_eq_repr {
( $type:ident::from_repr($number:literal), Some($enum:expr) ) => {
assert_eq!($type::from_repr($number), Some($enum));
if_rust_version! { >= 1.34 {
assert_eq!(core::convert::TryInto::<$type>::try_into($number), Ok($enum));
assert_eq!(<$type as core::convert::TryFrom<_>>::try_from($number), Ok($enum));
}}
};
( $type:ident::from_repr($number:literal), None ) => {
assert_eq!($type::from_repr($number), None);
if_rust_version! { >= 1.34 {
assert_eq!(core::convert::TryInto::<$type>::try_into($number), Err(::strum::ParseError::VariantNotFound));
assert_eq!(<$type as core::convert::TryFrom<_>>::try_from($number), Err(::strum::ParseError::VariantNotFound));
}}
};
}

#[test]
fn simple_test() {
assert_eq_repr!(Week::from_repr(0), Some(Week::Sunday));
assert_eq_repr!(Week::from_repr(1), Some(Week::Monday));
assert_eq_repr!(Week::from_repr(6), None);
assert_eq_repr!(Week::from_repr(7), Some(Week::Friday));
assert_eq_repr!(Week::from_repr(8), Some(Week::Saturday));
assert_eq_repr!(Week::from_repr(9), None);
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(1), Some(Week::Monday));
assert_eq!(Week::from_repr(6), None);
assert_eq!(Week::from_repr(7), Some(Week::Friday));
assert_eq!(Week::from_repr(8), Some(Week::Saturday));
assert_eq!(Week::from_repr(9), None);
}

#[rustversion::since(1.46)]
Expand Down Expand Up @@ -75,7 +57,7 @@ fn crate_module_path_test() {
Saturday,
}

assert_eq_repr!(Week::from_repr(0), Some(Week::Sunday));
assert_eq_repr!(Week::from_repr(6), Some(Week::Saturday));
assert_eq_repr!(Week::from_repr(7), None);
assert_eq!(Week::from_repr(0), Some(Week::Sunday));
assert_eq!(Week::from_repr(6), Some(Week::Saturday));
assert_eq!(Week::from_repr(7), None);
}
Empty file modified travis.sh
100755 → 100644
Empty file.