Skip to content

Commit 3091d8b

Browse files
committed
doc: split docs between README.md and the crate-level one, stop using cargo-readme
1 parent 4950789 commit 3091d8b

File tree

3 files changed

+6
-146
lines changed

3 files changed

+6
-146
lines changed

README.md

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ assert_eq!(try_match!(Var1(42), Var1(x) if x < 20 => x), Err(Var1(42)));
3030

3131
### Implicit Mapping
3232

33-
`=>` and the part that comes after can be omitted (requires `implicit_map`
34-
feature, which is enabled by default; you can disable it to skip the
35-
compilation of the internal procedural macro):
33+
`=>` and the following part can be omitted (requires `implicit_map`
34+
feature, which is enabled by default):
3635

3736
```rust
3837
// `()` if there are no bound variables
@@ -45,46 +44,10 @@ assert_eq!(try_match!(Var1(42), Var1(x) if x < 20), Err(Var1(42)));
4544
// An anonymous struct if there are multiple bound variables
4645
let vars = try_match!(Var1((12, 34)), Var1((a, b))).unwrap();
4746
assert_eq!((vars.a, vars.b), (12, 34));
48-
```
49-
50-
It produces a tuple if you name the bound variables like `_0`, `_1`, `_2`,
51-
...:
5247

53-
```rust
48+
// A tuple if the variable names are numeric
5449
let (a, b) = try_match!(Var1((12, 34)), Var1((_0, _1))).unwrap();
5550
assert_eq!((a, b), (12, 34));
56-
57-
try_match!(Var1((12, 34)), Var1((_0, _1)) if _0 == _1).unwrap_err();
58-
```
59-
60-
It's an error to specify non-contiguous binding indices:
61-
62-
```rust
63-
let _ = try_match!(Var1((12, 34)), Var1((_0, _2)));
64-
```
65-
66-
```rust
67-
let _ = try_match!(Var1((12, 34)), Var1((_0, _9223372036854775808)));
68-
```
69-
70-
## Quirks
71-
72-
When using implicit mapping, bind variables defined inside macros are
73-
not recognized because at the point of `try_match`'s macro expansion,
74-
inner macros are not expended yet.
75-
76-
This macro moves a value out of the place represented by the input
77-
expression to return it on failure. Make sure to pass a reference if this is
78-
not desired.
79-
80-
```rust
81-
let array = [Some(UncopyValue), None];
82-
// ERROR: Can't move out of `array[0]`
83-
let _: &UncopyValue = try_match!(array[0], Some(ref x)).unwrap();
84-
```
85-
86-
```rust
87-
let _: &UncopyValue = try_match!(&array[0], Some(x)).unwrap();
8851
```
8952

9053
## Applications
@@ -170,5 +133,6 @@ instead.
170133
[`bind_match::bind_match!`]: https://crates.io/crates/bind_match
171134
[`extract::extract!`]: https://crates.io/crates/extract_macro
172135

136+
## License
173137

174-
License: MIT/Apache-2.0
138+
MIT/Apache-2.0

README.tpl

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/lib.rs

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//!
2727
//! ## Implicit Mapping
2828
//!
29-
//! `=>` and the part that comes after can be omitted (requires `implicit_map`
29+
//! `=>` and the following part can be omitted (requires `implicit_map`
3030
//! feature, which is enabled by default; you can disable it to skip the
3131
//! compilation of the internal procedural macro):
3232
//!
@@ -199,103 +199,6 @@
199199
//! # let array = [Some(UncopyValue), None];
200200
//! let _: &UncopyValue = try_match!(&array[0], Some(x)).unwrap();
201201
//! ```
202-
//!
203-
//! # Applications
204-
//!
205-
//! ## `Iterator::filter_map`
206-
//!
207-
//! ```rust
208-
//! # use try_match::try_match;
209-
//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 }
210-
//! # use Enum::{Var1, Var2};
211-
//! let array = [Var1(42), Var2, Var1(10)];
212-
//! let filtered: Vec<_> = array
213-
//! .iter()
214-
//! .filter_map(|x| try_match!(x, &Var1(_0) if _0 > 20).ok())
215-
//! .collect();
216-
//! assert_eq!(filtered, [42]);
217-
//! ```
218-
//!
219-
//! ## `Iterator::map` + Fallible `Iterator::collect`
220-
//!
221-
//! ```rust
222-
//! # use try_match::try_match;
223-
//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 }
224-
//! # use Enum::{Var1, Var2};
225-
//! let array = [Var1(42), Var2, Var1(10)];
226-
//! let filtered: Result<Vec<_>, _> = array
227-
//! .iter()
228-
//! .map(|x| try_match!(x, &Var1(_0) if _0 > 20))
229-
//! .collect();
230-
//!
231-
//! // `Var2` is the first value that doesn't match
232-
//! assert_eq!(filtered, Err(&Var2));
233-
//! ```
234-
//!
235-
//! ## Extract Variants
236-
//!
237-
//! ```rust
238-
//! # use try_match::try_match;
239-
//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 }
240-
//! # use Enum::{Var1, Var2};
241-
//! impl<T> Enum<T> {
242-
//! fn var1(&self) -> Option<&T> {
243-
//! try_match!(self, Var1(_0)).ok()
244-
//! }
245-
//!
246-
//! fn is_var2(&self) -> bool {
247-
//! matches!(self, Var2)
248-
//! }
249-
//! }
250-
//!
251-
//! let enums = [Var1(42), Var2];
252-
//! assert_eq!(enums[0].var1(), Some(&42));
253-
//! assert_eq!(enums[1].var1(), None);
254-
//!
255-
//! assert!(!enums[0].is_var2());
256-
//! assert!(enums[1].is_var2());
257-
//! ```
258-
//!
259-
//! ## Expect Certain Variants
260-
//!
261-
//! ```rust
262-
//! # use try_match::try_match;
263-
//! # #[derive(Debug, PartialEq)] enum Enum<T> { Var1(T), Var2 }
264-
//! # use Enum::{Var1, Var2};
265-
//! fn this_fn_expects_var1(foo: &Enum<[u8; 4]>) {
266-
//! let (i0, i1) = try_match!(foo, &Var1([_0, _, _, _1])).unwrap();
267-
//!
268-
//! // Once RFC 1303 is stabilized, you can do instead:
269-
//! // let &Var1([i0, _, _, i1]) = foo else { panic!("{:?}", foo) };
270-
//!
271-
//! assert_eq!((i0, i1), (42, 45));
272-
//! }
273-
//!
274-
//! this_fn_expects_var1(&Var1([42, 43, 44, 45]));
275-
//! ```
276-
//!
277-
//! # Related Work
278-
//!
279-
//! [`matcher::matches!`][] (now incorporated into the standard library as
280-
//! [`core::matches!`][]) is similar but only returns `bool` indicating whether
281-
//! matching was successful or not.
282-
//!
283-
//! ```
284-
//! # use try_match::try_match;
285-
//! let success1 = matches!(Some(42), Some(_));
286-
//! let success2 = try_match!(Some(42), Some(_)).is_ok();
287-
//! assert_eq!(success1, success2);
288-
//! ```
289-
//!
290-
//! [`bind_match::bind_match!`][] and [`extract::extract!`][] use the same
291-
//! syntax (except for implicit mapping) but return `Some(expr)` on success
292-
//! instead.
293-
//!
294-
//! [`core::matches!`]: https://doc.rust-lang.org/1.56.0/core/macro.matches.html
295-
//! [`matcher::matches!`]: https://crates.io/crates/matches
296-
//! [`bind_match::bind_match!`]: https://crates.io/crates/bind_match
297-
//! [`extract::extract!`]: https://crates.io/crates/extract_macro
298-
//!
299202
#![no_std]
300203
#![forbid(unsafe_code)]
301204

0 commit comments

Comments
 (0)