Remove need for step_by feature
This commit is contained in:
parent
085800c330
commit
800b65622c
10
src/grid.rs
10
src/grid.rs
|
@ -27,7 +27,7 @@ use std::ops::{Deref, DerefMut, Range, RangeTo, RangeFrom, RangeFull, Index, Ind
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
use std::slice::{self, Iter, IterMut};
|
use std::slice::{self, Iter, IterMut};
|
||||||
|
|
||||||
use index::{self, Point};
|
use index::{self, Point, IndexRange};
|
||||||
|
|
||||||
/// Convert a type to a linear index range.
|
/// Convert a type to a linear index range.
|
||||||
pub trait ToRange {
|
pub trait ToRange {
|
||||||
|
@ -53,7 +53,7 @@ pub struct Grid<T> {
|
||||||
impl<T: Clone> Grid<T> {
|
impl<T: Clone> Grid<T> {
|
||||||
pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid<T> {
|
pub fn new(lines: index::Line, cols: index::Column, template: &T) -> Grid<T> {
|
||||||
let mut raw = Vec::with_capacity(*lines);
|
let mut raw = Vec::with_capacity(*lines);
|
||||||
for _ in index::Line(0)..lines {
|
for _ in IndexRange(index::Line(0)..lines) {
|
||||||
raw.push(Row::new(cols, template));
|
raw.push(Row::new(cols, template));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ impl<T: Clone> Grid<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn grow_lines(&mut self, lines: index::Line, template: &T) {
|
fn grow_lines(&mut self, lines: index::Line, template: &T) {
|
||||||
for _ in self.num_lines()..lines {
|
for _ in IndexRange(self.num_lines()..lines) {
|
||||||
self.raw.push(Row::new(self.cols, template));
|
self.raw.push(Row::new(self.cols, template));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ impl<T> Grid<T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn scroll_down(&mut self, region: Range<index::Line>, positions: index::Line) {
|
pub fn scroll_down(&mut self, region: Range<index::Line>, positions: index::Line) {
|
||||||
for line in region.rev() {
|
for line in IndexRange(region).rev() {
|
||||||
let src = line;
|
let src = line;
|
||||||
let dst = line - positions;
|
let dst = line - positions;
|
||||||
self.swap_lines(src, dst);
|
self.swap_lines(src, dst);
|
||||||
|
@ -133,7 +133,7 @@ impl<T> Grid<T> {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn scroll_up(&mut self, region: Range<index::Line>, positions: index::Line) {
|
pub fn scroll_up(&mut self, region: Range<index::Line>, positions: index::Line) {
|
||||||
for line in region {
|
for line in IndexRange(region) {
|
||||||
let src = line;
|
let src = line;
|
||||||
let dst = line + positions;
|
let dst = line + positions;
|
||||||
self.swap_lines(src, dst);
|
self.swap_lines(src, dst);
|
||||||
|
|
69
src/index.rs
69
src/index.rs
|
@ -17,9 +17,7 @@
|
||||||
/// Indexing types and implementations for Grid and Line
|
/// Indexing types and implementations for Grid and Line
|
||||||
use std::cmp::{Ord, Ordering};
|
use std::cmp::{Ord, Ordering};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::iter::Step;
|
use std::ops::{self, Deref, Add, Range};
|
||||||
use std::mem;
|
|
||||||
use std::ops::{self, Deref, Add};
|
|
||||||
|
|
||||||
/// The side of a cell
|
/// The side of a cell
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
@ -200,6 +198,18 @@ macro_rules! sub {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This exists because we can't implement Iterator on Range
|
||||||
|
/// and the existing impl needs the unstable Step trait
|
||||||
|
/// This should be removed and replaced with a Step impl
|
||||||
|
/// in the ops macro when `step_by` is stabilized
|
||||||
|
pub struct IndexRange<T>(pub Range<T>);
|
||||||
|
|
||||||
|
impl<T> From<Range<T>> for IndexRange<T> {
|
||||||
|
fn from(from: Range<T>) -> Self {
|
||||||
|
IndexRange(from)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! ops {
|
macro_rules! ops {
|
||||||
($ty:ty, $construct:expr) => {
|
($ty:ty, $construct:expr) => {
|
||||||
add!($ty, $construct);
|
add!($ty, $construct);
|
||||||
|
@ -207,12 +217,7 @@ macro_rules! ops {
|
||||||
deref!($ty, usize);
|
deref!($ty, usize);
|
||||||
forward_ref_binop!(impl Add, add for $ty, $ty);
|
forward_ref_binop!(impl Add, add for $ty, $ty);
|
||||||
|
|
||||||
impl Step for $ty {
|
impl $ty {
|
||||||
#[inline]
|
|
||||||
fn step(&self, by: &$ty) -> Option<$ty> {
|
|
||||||
Some(*self + *by)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(trivial_numeric_casts)]
|
#[allow(trivial_numeric_casts)]
|
||||||
fn steps_between(start: &$ty, end: &$ty, by: &$ty) -> Option<usize> {
|
fn steps_between(start: &$ty, end: &$ty, by: &$ty) -> Option<usize> {
|
||||||
|
@ -233,33 +238,41 @@ macro_rules! ops {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn steps_between_by_one(start: &$ty, end: &$ty) -> Option<usize> {
|
fn steps_between_by_one(start: &$ty, end: &$ty) -> Option<usize> {
|
||||||
Step::steps_between(start, end, &$construct(1))
|
Self::steps_between(start, end, &$construct(1))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for IndexRange<$ty> {
|
||||||
|
type Item = $ty;
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(unused_comparisons)]
|
fn next(&mut self) -> Option<$ty> {
|
||||||
fn is_negative(&self) -> bool {
|
if self.0.start < self.0.end {
|
||||||
self.0 < 0
|
let old = self.0.start;
|
||||||
|
self.0.start = old + 1;
|
||||||
|
Some(old)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn replace_one(&mut self) -> Self {
|
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||||
mem::replace(self, $construct(0))
|
match Self::Item::steps_between_by_one(&self.0.start, &self.0.end) {
|
||||||
|
Some(hint) => (hint, Some(hint)),
|
||||||
|
None => (0, None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DoubleEndedIterator for IndexRange<$ty> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn replace_zero(&mut self) -> Self {
|
fn next_back(&mut self) -> Option<$ty> {
|
||||||
mem::replace(self, $construct(1))
|
if self.0.start < self.0.end {
|
||||||
}
|
let new = self.0.end - 1;
|
||||||
|
self.0.end = new;
|
||||||
#[inline]
|
Some(new)
|
||||||
fn add_one(&self) -> Self {
|
} else {
|
||||||
*self + 1
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
fn sub_one(&self) -> Self {
|
|
||||||
*self - 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@
|
||||||
#![feature(inclusive_range_syntax)]
|
#![feature(inclusive_range_syntax)]
|
||||||
#![feature(inclusive_range)]
|
#![feature(inclusive_range)]
|
||||||
#![feature(drop_types_in_const)]
|
#![feature(drop_types_in_const)]
|
||||||
#![feature(step_trait)]
|
|
||||||
#![cfg_attr(feature = "clippy", feature(plugin))]
|
#![cfg_attr(feature = "clippy", feature(plugin))]
|
||||||
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
#![cfg_attr(feature = "clippy", plugin(clippy))]
|
||||||
#![cfg_attr(feature = "clippy", deny(clippy))]
|
#![cfg_attr(feature = "clippy", deny(clippy))]
|
||||||
|
|
|
@ -20,7 +20,7 @@ use std::io;
|
||||||
|
|
||||||
use ansi::{self, Color, NamedColor, Attr, Handler};
|
use ansi::{self, Color, NamedColor, Attr, Handler};
|
||||||
use grid::{Grid, ClearRegion, ToRange};
|
use grid::{Grid, ClearRegion, ToRange};
|
||||||
use index::{self, Point, Column, Line, Linear};
|
use index::{self, Point, Column, Line, Linear, IndexRange};
|
||||||
use selection::{Span, Selection};
|
use selection::{Span, Selection};
|
||||||
|
|
||||||
pub mod cell;
|
pub mod cell;
|
||||||
|
@ -286,7 +286,7 @@ impl Term {
|
||||||
|
|
||||||
let grid = Grid::new(num_lines, num_cols, &template);
|
let grid = Grid::new(num_lines, num_cols, &template);
|
||||||
|
|
||||||
let mut tabs = (Column(0)..grid.num_cols())
|
let mut tabs = IndexRange::from(Column(0)..grid.num_cols())
|
||||||
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
||||||
.collect::<Vec<bool>>();
|
.collect::<Vec<bool>>();
|
||||||
|
|
||||||
|
@ -424,7 +424,7 @@ impl Term {
|
||||||
// Starting line
|
// Starting line
|
||||||
res.append(&self.grid, start.line, start.col..);
|
res.append(&self.grid, start.line, start.col..);
|
||||||
|
|
||||||
let middle_range = (start.line + 1)..(end.line);
|
let middle_range = IndexRange::from((start.line + 1)..(end.line));
|
||||||
for line in middle_range {
|
for line in middle_range {
|
||||||
res.append(&self.grid, line, ..);
|
res.append(&self.grid, line, ..);
|
||||||
}
|
}
|
||||||
|
@ -507,7 +507,7 @@ impl Term {
|
||||||
self.cursor.col = limit(self.cursor.col, Column(0), num_cols);
|
self.cursor.col = limit(self.cursor.col, Column(0), num_cols);
|
||||||
|
|
||||||
// Recreate tabs list
|
// Recreate tabs list
|
||||||
self.tabs = (Column(0)..self.grid.num_cols())
|
self.tabs = IndexRange::from(Column(0)..self.grid.num_cols())
|
||||||
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
.map(|i| (*i as usize) % TAB_SPACES == 0)
|
||||||
.collect::<Vec<bool>>();
|
.collect::<Vec<bool>>();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue