Skip to content

Commit

Permalink
impl review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Aug 13, 2017
1 parent 200e721 commit cf50802
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions unic/char/range/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn forward_iteration(b: &mut test::Bencher) {

#[bench]
fn forward_iteration_baseline(b: &mut test::Bencher) {
b.iter(|| (0..0x110000).filter_map(char::from_u32).count())
b.iter(|| (0..0x11_0000).filter_map(char::from_u32).count())
}

#[bench]
Expand All @@ -23,7 +23,7 @@ fn reverse_iteration(b: &mut test::Bencher) {

#[bench]
fn reverse_iteration_baseline(b: &mut test::Bencher) {
b.iter(|| (0..0x110000).rev().filter_map(char::from_u32).count())
b.iter(|| (0..0x11_0000).rev().filter_map(char::from_u32).count())
}

#[bench]
Expand Down
2 changes: 2 additions & 0 deletions unic/char/range/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use {step, CharRange};
const SURROGATE_RANGE: Range<u32> = 0xD800..0xE000;

/// An iterator over a range of unicode code points.
///
/// Constructed via `CharRange::iter`. See `CharRange` for more information.
#[derive(Clone, Debug)]
pub struct CharIter {
/// The lowest uniterated character (inclusive).
Expand Down
10 changes: 5 additions & 5 deletions unic/char/range/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
//! # Examples
//!
//! ```
//! # #[macro_use] extern crate unic_char_range;
//! # use unic_char_range::*;
//! #[macro_use] extern crate unic_char_range;
//!
//! # fn main() {
//! for character in chars!('a'..='z') {
//! // character is each character in the lowercase english alphabet in order
Expand All @@ -27,9 +27,9 @@
//! - `fused`: impl the [`FusedIterator`] contract
//! - `trusted-len`: impl the [`TrustedLen`] contract
//!
//! [is_empty](https://doc.rust-lang.org/std/iter/trait.ExactSizeIterator.html#method.is_empty)
//! [`FusedIterator`](https://doc.rust-lang.org/std/iter/trait.FusedIterator.html)
//! [`TrustedLen`](https://doc.rust-lang.org/std/iter/trait.TrustedLen.html)
//! [is_empty]: https://doc.rust-lang.org/std/iter/trait.ExactSizeIterator.html#method.is_empty
//! [`FusedIterator`]: https://doc.rust-lang.org/std/iter/trait.FusedIterator.html
//! [`TrustedLen`]: https://doc.rust-lang.org/std/iter/trait.TrustedLen.html
//!
#![forbid(bad_style, missing_debug_implementations, unconditional_recursion)]
#![deny(missing_docs, unsafe_code, unused, future_incompatible)]
Expand Down
30 changes: 19 additions & 11 deletions unic/char/range/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ use CharIter;
/// The most idiomatic way to construct this range is through the use of the `chars!` macro:
///
/// ```
/// # #[macro_use] extern crate unic_char_range;
/// # use unic_char_range::*;
/// #[macro_use] extern crate unic_char_range;
/// use unic_char_range::CharRange;
///
/// # fn main() {
/// assert_eq!(chars!('a'..='z'), CharRange::closed('a', 'z'));
/// assert_eq!(chars!('a'..'z'), CharRange::open_right('a', 'z'));
/// assert_eq!(chars!(..), CharRange::all());
/// # }
/// ```
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
///
/// If constructed in reverse order, such that `self.high` is ordered before `self.low`,
/// the range is empty. If you want to iterate in decreasing order, use `.iter().rev()`.
/// All empty ranges are considered equal no matter the internal state.
#[derive(Copy, Clone, Debug, Eq)]
pub struct CharRange {
/// The lowest character in this range (inclusive).
pub low: char,
Expand Down Expand Up @@ -48,8 +53,6 @@ impl CharRange {

/// Construct a half open (right) range of characters.
///
/// If `stop` is ordered before `start`, the resulting range will be empty.
///
/// # Example
///
/// ```
Expand All @@ -67,8 +70,6 @@ impl CharRange {

/// Construct a half open (left) range of characters.
///
/// If `stop` is ordered before `start`, the resulting range will be empty.
///
/// # Example
///
/// ```
Expand All @@ -86,8 +87,6 @@ impl CharRange {

/// Construct a fully open range of characters.
///
/// If `stop` is ordered before `start`, the resulting range will be empty.
///
/// # Example
///
/// ```
Expand All @@ -105,8 +104,6 @@ impl CharRange {
}

/// Construct a range of characters from bounds.
///
/// If `stop` is ordered before `start`, the resulting range will be empty.
pub fn bound(start: Bound<char>, stop: Bound<char>) -> CharRange {
let start = if start == Bound::Unbounded {
Bound::Included('\0')
Expand Down Expand Up @@ -156,6 +153,11 @@ impl CharRange {
self.iter().len()
}

/// Is this range empty?
pub fn is_empty(&self) -> bool {
self.low > self.high
}

/// Create an iterator over this range.
pub fn iter(&self) -> CharIter {
(*self).into()
Expand All @@ -170,3 +172,9 @@ impl IntoIterator for CharRange {
self.iter()
}
}

impl PartialEq<CharRange> for CharRange {
fn eq(&self, other: &CharRange) -> bool {
(self.is_empty() && other.is_empty()) || (self.low == other.low && self.high == other.high)
}
}

0 comments on commit cf50802

Please sign in to comment.