From dca9d7f662b16f9ffe6828094cd6b8e5ee6e8ea8 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 25 May 2015 14:46:29 -0700 Subject: [PATCH] Use usize for Vec2 instead of u32 Much less casts needed now. --- src/lib.rs | 4 ++-- src/margins.rs | 14 +++++++------- src/printer.rs | 6 +++--- src/vec.rs | 30 +++++++++++++++--------------- src/view/button.rs | 6 +++--- src/view/dialog.rs | 6 +++--- src/view/mod.rs | 6 +++--- src/view/text_view.rs | 8 ++++---- 8 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 24e5411..0013a4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -176,8 +176,8 @@ impl Cursive { ncurses::getmaxyx(ncurses::stdscr, &mut y, &mut x); Vec2 { - x: x as u32, - y: y as u32, + x: x as usize, + y: y as usize, } } diff --git a/src/margins.rs b/src/margins.rs index 5376005..e1a43a2 100644 --- a/src/margins.rs +++ b/src/margins.rs @@ -3,18 +3,18 @@ use vec::Vec2; /// Fixed margins around a rectangular view. pub struct Margins { /// Left margin - pub left: u32, + pub left: usize, /// Right margin - pub right: u32, + pub right: usize, /// Top margin - pub top: u32, + pub top: usize, /// Bottom margin - pub bottom: u32, + pub bottom: usize, } impl Margins { /// Creates new margins. - pub fn new(left: u32, right: u32, top: u32, bottom: u32) -> Self { + pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self { Margins { left: left, right: right, @@ -24,12 +24,12 @@ impl Margins { } /// Returns left + right. - pub fn horizontal(&self) -> u32 { + pub fn horizontal(&self) -> usize { self.left + self.right } /// Returns top + bottom. - pub fn vertical(&self) -> u32 { + pub fn vertical(&self) -> usize { self.top + self.bottom } diff --git a/src/printer.rs b/src/printer.rs index 4f86948..278c71c 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -29,7 +29,7 @@ impl Printer { let p = pos.to_vec2(); if p.y >= self.size.y || p.x >= self.size.x { return; } // Do we have enough room for the entire line? - let room = (self.size.x - p.x) as usize; + let room = self.size.x - p.x; // We want the number of CHARACTERS, not bytes. let text = match text.char_indices().nth(room) { Some((i,_)) => &text[..i], @@ -40,7 +40,7 @@ impl Printer { } /// Prints a vertical line using the given character. - pub fn print_vline(&self, start: T, len: u32, c: u64) { + pub fn print_vline(&self, start: T, len: usize, c: u64) { let p = start.to_vec2(); if p.y > self.size.y || p.x > self.size.x { return; } let len = min(len, self.size.y - p.y); @@ -50,7 +50,7 @@ impl Printer { } /// Prints a horizontal line using the given character. - pub fn print_hline(&self, start: T, len: u32, c: u64) { + pub fn print_hline(&self, start: T, len: usize, c: u64) { let p = start.to_vec2(); if p.y > self.size.y || p.x > self.size.x { return; } let len = min(len, self.size.x - p.x); diff --git a/src/vec.rs b/src/vec.rs index de16d1c..445b632 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -7,14 +7,14 @@ use std::cmp::{min,max}; #[derive(Clone,Copy)] pub struct Vec2 { /// X coordinate (column), from left to right. - pub x: u32, + pub x: usize, /// Y coordinate (row), from top to bottom. - pub y: u32, + pub y: usize, } impl Vec2 { /// Creates a new Vec2 from coordinates. - pub fn new(x: u32, y: u32) -> Self { + pub fn new(x: usize, y: usize) -> Self { Vec2 { x: x, y: y, @@ -61,19 +61,19 @@ impl ToVec2 for Vec2 { impl ToVec2 for (i32,i32) { fn to_vec2(self) -> Vec2 { - (self.0 as u32, self.1 as u32).to_vec2() - } -} - -impl ToVec2 for (u32,u32) { - fn to_vec2(self) -> Vec2 { - Vec2::new(self.0, self.1) + (self.0 as usize, self.1 as usize).to_vec2() } } impl ToVec2 for (usize,usize) { fn to_vec2(self) -> Vec2 { - Vec2::new(self.0 as u32, self.1 as u32) + Vec2::new(self.0, self.1) + } +} + +impl ToVec2 for (u32,u32) { + fn to_vec2(self) -> Vec2 { + Vec2::new(self.0 as usize, self.1 as usize) } } @@ -101,10 +101,10 @@ impl Sub for Vec2 { } } -impl Div for Vec2 { +impl Div for Vec2 { type Output = Vec2; - fn div(self, other: u32) -> Vec2 { + fn div(self, other: usize) -> Vec2 { Vec2 { x: self.x / other, y: self.y / other, @@ -112,10 +112,10 @@ impl Div for Vec2 { } } -impl Mul for Vec2 { +impl Mul for Vec2 { type Output = Vec2; - fn mul(self, other: u32) -> Vec2 { + fn mul(self, other: usize) -> Vec2 { Vec2 { x: self.x * other, y: self.y * other, diff --git a/src/view/button.rs b/src/view/button.rs index c715748..e5656a3 100644 --- a/src/view/button.rs +++ b/src/view/button.rs @@ -33,15 +33,15 @@ impl View for Button { let x = printer.size.x - 1; printer.with_style(style, |printer| { - printer.print((1u32,0u32), &self.label); - printer.print((0u32,0u32), "<"); + printer.print((1,0), &self.label); + printer.print((0,0), "<"); printer.print((x,0), ">"); }); } fn get_min_size(&self, _: SizeRequest) -> Vec2 { // Meh. Fixed size we are. - Vec2::new(2 + self.label.len() as u32, 1) + Vec2::new(2 + self.label.len(), 1) } fn on_key_event(&mut self, ch: i32) -> EventResult { diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 4175394..7ceff90 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -102,9 +102,9 @@ impl View for Dialog { printer.print_box(Vec2::new(0,0), printer.size); if self.title.len() > 0 { - let x = (printer.size.x - self.title.len() as u32) / 2; + let x = (printer.size.x - self.title.len()) / 2; printer.print((x-2,0), "┤ "); - printer.print((x+self.title.len() as u32,0), " ├"); + printer.print((x+self.title.len(),0), " ├"); printer.with_style(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title)); } @@ -131,7 +131,7 @@ impl View for Dialog { if self.title.len() > 0 { // If we have a title, we have to fit it too! - inner_size.x = max(inner_size.x, self.title.len() as u32 + 6); + inner_size.x = max(inner_size.x, self.title.len() + 6); } inner_size diff --git a/src/view/mod.rs b/src/view/mod.rs index db938a4..676b788 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -65,16 +65,16 @@ pub enum Selector<'a> { #[derive(PartialEq,Clone,Copy)] pub enum DimensionRequest { /// The view must use exactly the attached size. - Fixed(u32), + Fixed(usize), /// The view is free to choose its size if it stays under the limit. - AtMost(u32), + AtMost(usize), /// No clear restriction apply. Unknown, } impl DimensionRequest { /// Returns a new request, reduced from the original by the given offset. - pub fn reduced(self, offset: u32) -> Self { + pub fn reduced(self, offset: usize) -> Self { match self { DimensionRequest::Fixed(w) => DimensionRequest::Fixed(w - offset), DimensionRequest::AtMost(w) => DimensionRequest::AtMost(w - offset), diff --git a/src/view/text_view.rs b/src/view/text_view.rs index 4ad1903..175aa28 100644 --- a/src/view/text_view.rs +++ b/src/view/text_view.rs @@ -89,7 +89,7 @@ impl TextView { for line in self.content.split("\n") { height += 1; - max_width = cmp::max(max_width, line.len() as u32); + max_width = cmp::max(max_width, line.len()); } Vec2::new(max_width, height) @@ -182,11 +182,11 @@ impl View for TextView { // TODO: what if the text has newlines?? (DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(), (DimensionRequest::Fixed(w),_) => { - let h = self.get_num_lines(w as usize) as u32; + let h = self.get_num_lines(w); Vec2::new(w, h) }, (_,DimensionRequest::Fixed(h)) => { - let w = self.get_num_cols(h as usize) as u32; + let w = self.get_num_cols(h); Vec2::new(w, h) }, (DimensionRequest::AtMost(w),_) => { @@ -196,7 +196,7 @@ impl View for TextView { if w >= ideal.x { ideal } else { - let h = self.get_num_lines(w as usize) as u32; + let h = self.get_num_lines(w); Vec2::new(w, h) } },