Skip to content

Commit

Permalink
Updated documents
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-glotfelty committed Dec 24, 2018
1 parent fe6c20a commit e79a6fe
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 45 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
@@ -1,6 +1,13 @@
# Changelog

## Unreleased
## 0.13.0

### Added

* Added a derive to implement `From<YourEnum>` for `&'static str`. This deprecates `AsStaticStr` since
the new solution doesn't require a `strum` specific trait to use.

## 0.12.0

### Added

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2017 Peter Glotfelty
Copyright (c) 2018 Peter Glotfelty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
41 changes: 31 additions & 10 deletions README.md
Expand Up @@ -14,8 +14,8 @@ Cargo.toml. Strum_macros contains the macros needed to derive all the traits in

```toml
[dependencies]
strum = "0.12.0"
strum_macros = "0.12.0"
strum = "0.13.0"
strum_macros = "0.13.0"
```

And add these lines to the root of your project, either lib.rs or main.rs.
Expand Down Expand Up @@ -135,17 +135,15 @@ Strum has implemented the following macros:
`ToString` for determining what string is returned. The difference is that `as_ref()` returns
a `&str` instead of a `String` so you don't allocate any additional memory with each call.

4. `AsStaticStr`: this is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful
in some scenarios. This macro implements `strum::AsStaticRef<str>` which adds a method `.to_static()` that
returns a `&'static str`.
4. `IntoStaticStr`: this trait implements `From<YourEnum>` and `From<&'a YourEnum>` for `&'static str`. This is
useful for turning an enum variant into a static string. The Rust `std` provides a blanket impl of the
reverse direction - i.e. `impl Into<&'static str> for YourEnum`.

```rust
extern crate strum;
extern crate strum;
#[macro_use] extern crate strum_macros;

use strum::AsStaticRef;

#[derive(AsStaticStr)]
#[derive(IntoStaticStr)]
enum State<'a> {
Initial(&'a str),
Finished
Expand All @@ -155,13 +153,36 @@ Strum has implemented the following macros:
let state = State::Initial(s);
// The following won't work because the lifetime is incorrect so we can use.as_static() instead.
// let wrong: &'static str = state.as_ref();
let right: &'static str = state.as_static();
let right: &'static str = state.into();
println!("{}", right);
}

fn main() {
print_state(&"hello world".to_string())
}
```

4. `AsStaticStr`: **Deprecated since version 0.13.0. Prefer IntoStaticStr instead.**
This is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful
in some scenarios. This macro implements `strum::AsStaticRef<str>` which adds a method `.to_static()` that
returns a `&'static str`.

```rust
extern crate strum;
#[macro_use] extern crate strum_macros;

use strum::AsStaticRef;

#[derive(AsStaticStr)]
enum State<'a> {
Initial(&'a str),
Finished
}

fn print_state<'a>(s:&'a str) {
let right: &'static str = State::Initial(s).as_static();
println!("{}", right);
}
```

4. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
Expand Down
4 changes: 2 additions & 2 deletions strum/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "strum"
version = "0.12.0"
version = "0.13.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
license = "MIT"

Expand All @@ -13,7 +13,7 @@ homepage = "https://github.com/Peternator7/strum"
readme = "../README.md"

[dev-dependencies]
strum_macros = { path = "../strum_macros", version = "0.12.0" }
strum_macros = { path = "../strum_macros", version = "0.13.0" }

[badges]
travis-ci = { repository = "Peternator7/strum" }
57 changes: 28 additions & 29 deletions strum/src/lib.rs
Expand Up @@ -14,8 +14,8 @@
//!
//! ```toml
//! [dependencies]
//! strum = "0.11.0"
//! strum_macros = "0.11.0"
//! strum = "0.13.0"
//! strum_macros = "0.13.0"
//! ```
//!
//! And add these lines to the root of your project, either lib.rs or main.rs.
Expand Down Expand Up @@ -122,34 +122,33 @@
//! 3. `AsRefStr`: this derive implements `AsRef<str>` on your enum using the same rules as
//! `ToString` for determining what string is returned. The difference is that `as_ref()` returns
//! a borrowed `str` instead of a `String` so you can save an allocation.
//!
//! 4. `IntoStaticStr`: this trait implements `From<YourEnum>` and `From<&'a YourEnum>` for `&'static str`. This is
//! useful for turning an enum variant into a static string. The Rust `std` provides a blanket impl of the
//! reverse direction - i.e. `impl Into<&'static str> for YourEnum`.
//!
//! 4. `AsStaticStr`: this is similar to `AsRefStr`, but returns a `'static` reference to a string which is helpful
//! in some scenarios. This macro implements `strum::AsStaticRef<str>` which adds a method `.as_static()` that
//! returns a `&'static str`.
//!
//! ```rust
//! # extern crate strum;
//! # #[macro_use] extern crate strum_macros;
//! use strum::AsStaticRef;
//!
//! #[derive(AsStaticStr)]
//! enum State<'a> {
//! Initial(&'a str),
//! Finished
//! }
//!
//! fn print_state<'a>(s:&'a str) {
//! let state = State::Initial(s);
//! // The following won't work because the lifetime is incorrect so we can use.as_static() instead.
//! // let wrong: &'static str = state.as_ref();
//! let right: &'static str = state.as_static();
//! println!("{}", right);
//! }
//!
//! fn main() {
//! print_state(&"hello world".to_string())
//! }
//! ```
//! ```rust
//! extern crate strum;
//! #[macro_use] extern crate strum_macros;
//!
//! #[derive(IntoStaticStr)]
//! enum State<'a> {
//! Initial(&'a str),
//! Finished
//! }
//!
//! fn print_state<'a>(s:&'a str) {
//! let state = State::Initial(s);
//! // The following won't work because the lifetime is incorrect so we can use.as_static() instead.
//! // let wrong: &'static str = state.as_ref();
//! let right: &'static str = state.into();
//! println!("{}", right);
//! }
//!
//! fn main() {
//! print_state(&"hello world".to_string())
//! }
//! ```
//!
//! 4. `EnumIter`: iterate over the variants of an Enum. Any additional data on your variants will be
//! set to `Default::default()`. The macro implements `strum::IntoEnumIter` on your enum and
Expand Down
2 changes: 1 addition & 1 deletion strum_macros/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "strum_macros"
version = "0.12.0"
version = "0.13.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]
license = "MIT"

Expand Down
2 changes: 1 addition & 1 deletion strum_tests/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "strum_tests"
version = "0.12.0"
version = "0.13.0"
authors = ["Peter Glotfelty <peglotfe@microsoft.com>"]

[dependencies]
Expand Down

0 comments on commit e79a6fe

Please sign in to comment.