Skip to content

Commit

Permalink
Add polyfill matches! for old environments
Browse files Browse the repository at this point in the history
  • Loading branch information
statiolake committed Apr 27, 2021
1 parent 7754eeb commit 7064056
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/pcl/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! 実際のドキュメントはクレートのルートに配置されている。
//!
//! - [`rtl!`](../../macro.rtl.html) ― 複合代入演算子を右辺から評価するマクロ。
//! - [`matches!`](../../macro.matches.html) ― 標準の matches! と同様のもの (polyfill) 。
/// 複合代入演算子を右辺から評価するマクロ。
///
Expand Down Expand Up @@ -79,3 +80,17 @@ macro_rules! rtl {
rtl!(@lhs () @rest $($rest)*)
};
}

/// 値がパターンにマッチするかどうかを返すマクロ。Rust 1.42 から標準入りした `matches!` と全く同様な
/// ので、Rust 1.42 以降をターゲットとする場合には見えないようになっている。
// そのまま標準ライブラリからコピーしてきた。
#[cfg(not(feature = "rust-142"))]
#[macro_export]
macro_rules! matches {
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )? $(,)?) => {
match $expression {
$( $pattern )|+ $( if $guard )? => true,
_ => false
}
}
}
3 changes: 3 additions & 0 deletions src/pcl/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ pub use std::collections::*;
pub use std::f64::consts::*;
pub use std::{f32, f64, i16, i32, i64, i8, isize, u16, u32, u64, u8, usize};

#[cfg(not(feature = "rust-142"))]
pub use crate::matches;

#[cfg(feature = "crates-atc-2020")]
mod prelude_atc_2020;
#[cfg(feature = "crates-atc-2020")]
Expand Down
3 changes: 3 additions & 0 deletions src/pcl/structure/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ mod tests {

#[test]
fn test_tree() {
#[cfg(not(feature = "rust-142"))]
use crate::matches;

let mut graph = UndirectedAdjacencyList::<i32>::of_size(9);
let edges = [(0, 2), (0, 3), (1, 4), (1, 5), (1, 6), (2, 7), (2, 8)];
graph.add_edges(edges.iter().copied());
Expand Down

0 comments on commit 7064056

Please sign in to comment.