|
| 1 | +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +// ASCII art shape renderer. |
| 12 | +// Demonstrates traits, impls, operator overloading, non-copyable struct, unit testing. |
| 13 | +// To run execute: rustc --test shapes.rs && ./shapes |
| 14 | + |
| 15 | +// Rust's core library is tightly bound to the language itself so it is automatically linked in. |
| 16 | +// However the std library is designed to be optional (for code that must run on constrained |
| 17 | +// environments like embedded devices or special environments like kernel code) so it must |
| 18 | +// be explicitly linked in. |
| 19 | +extern mod std; |
| 20 | + |
| 21 | +// Extern mod controls linkage. Use controls the visibility of names to modules that are |
| 22 | +// already linked in. Using WriterUtil allows us to use the write_line method. |
| 23 | +use io::WriterUtil; |
| 24 | + |
| 25 | +// Represents a position on a canvas. |
| 26 | +struct Point |
| 27 | +{ |
| 28 | + x: int, |
| 29 | + y: int, |
| 30 | +} |
| 31 | + |
| 32 | +// Represents an offset on a canvas. (This has the same structure as a Point. |
| 33 | +// but different semantics). |
| 34 | +struct Size |
| 35 | +{ |
| 36 | + width: int, |
| 37 | + height: int, |
| 38 | +} |
| 39 | + |
| 40 | +struct Rect |
| 41 | +{ |
| 42 | + top_left: Point, |
| 43 | + size: Size, |
| 44 | +} |
| 45 | + |
| 46 | +// TODO: operators |
| 47 | + |
| 48 | +// Contains the information needed to do shape rendering via ASCII art. |
| 49 | +struct AsciiArt |
| 50 | +{ |
| 51 | + width: uint, |
| 52 | + height: uint, |
| 53 | + priv fill: char, |
| 54 | + priv lines: ~[~[mut char]], |
| 55 | + |
| 56 | + // This struct can be quite large so we'll disable copying: developers need |
| 57 | + // to either pass these structs around via borrowed pointers or move them. |
| 58 | + drop {} |
| 59 | +} |
| 60 | + |
| 61 | +// It's common to define a constructor sort of function to create struct instances. |
| 62 | +// If there is a canonical constructor it is typically named the same as the type. |
| 63 | +// Other constructor sort of functions are typically named from_foo, from_bar, etc. |
| 64 | +fn AsciiArt(width: uint, height: uint, fill: char) -> AsciiArt |
| 65 | +{ |
| 66 | + // Use an anonymous function to build a vector of vectors containing |
| 67 | + // blank characters for each position in our canvas. |
| 68 | + let lines = do vec::build_sized(height) |
| 69 | + |push| |
| 70 | + { |
| 71 | + for height.times |
| 72 | + { |
| 73 | + let mut line = ~[]; |
| 74 | + vec::grow_set(&mut line, width-1, &'.', '.'); |
| 75 | + push(vec::to_mut(line)); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + // Rust code often returns values by omitting the trailing semi-colon |
| 80 | + // instead of using an explicit return statement. |
| 81 | + AsciiArt {width: width, height: height, fill: fill, lines: lines} |
| 82 | +} |
| 83 | + |
| 84 | +// Methods particular to the AsciiArt struct. |
| 85 | +impl AsciiArt |
| 86 | +{ |
| 87 | + fn add_pt(x: int, y: int) |
| 88 | + { |
| 89 | + if x >= 0 && x < self.width as int |
| 90 | + { |
| 91 | + if y >= 0 && y < self.height as int |
| 92 | + { |
| 93 | + // Note that numeric types don't implicitly convert to each other. |
| 94 | + let v = y as uint; |
| 95 | + let h = x as uint; |
| 96 | + |
| 97 | + // Vector subscripting will normally copy the element, but &v[i] |
| 98 | + // will return a reference which is what we need because the |
| 99 | + // element is: |
| 100 | + // 1) potentially large |
| 101 | + // 2) needs to be modified |
| 102 | + let row = &self.lines[v]; |
| 103 | + row[h] = self.fill; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Allows AsciiArt to be converted to a string using the libcore ToStr trait. |
| 110 | +// Note that the %s fmt! specifier will not call this automatically. |
| 111 | +impl AsciiArt : ToStr |
| 112 | +{ |
| 113 | + pure fn to_str() -> ~str |
| 114 | + { |
| 115 | + // Convert each line into a string. |
| 116 | + let lines = do self.lines.map |line| {str::from_chars(*line)}; |
| 117 | + |
| 118 | + // Concatenate the lines together using a new-line. |
| 119 | + str::connect(lines, "\n") |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// This is similar to an interface in other languages: it defines a protocol which |
| 124 | +// developers can implement for arbitrary concrete types. |
| 125 | +#[allow(default_methods)] |
| 126 | +trait Canvas |
| 127 | +{ |
| 128 | + fn add_point(shape: Point); |
| 129 | + fn add_rect(shape: Rect); |
| 130 | + |
| 131 | + // Unlike interfaces traits support default implementations. |
| 132 | + // Got an ICE as soon as I added this method. |
| 133 | + fn add_points(shapes: &[Point]) |
| 134 | + { |
| 135 | + for shapes.each |pt| {self.add_point(*pt)}; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +// Here we provide an implementation of the Canvas methods for AsciiArt. |
| 140 | +// Other implementations could also be provided (e.g. for PDF or Apple's Quartz) |
| 141 | +// and code can use them polymorphically via the Canvas trait. |
| 142 | +impl AsciiArt : Canvas |
| 143 | +{ |
| 144 | + fn add_point(shape: Point) |
| 145 | + { |
| 146 | + self.add_pt(shape.x, shape.y); |
| 147 | + } |
| 148 | + |
| 149 | + fn add_rect(shape: Rect) |
| 150 | + { |
| 151 | + // Add the top and bottom lines. |
| 152 | + for int::range(shape.top_left.x, shape.top_left.x + shape.size.width) |
| 153 | + |x| |
| 154 | + { |
| 155 | + self.add_pt(x, shape.top_left.y); |
| 156 | + self.add_pt(x, shape.top_left.y + shape.size.height - 1); |
| 157 | + } |
| 158 | + |
| 159 | + // Add the left and right lines. |
| 160 | + for int::range(shape.top_left.y, shape.top_left.y + shape.size.height) |
| 161 | + |y| |
| 162 | + { |
| 163 | + self.add_pt(shape.top_left.x, y); |
| 164 | + self.add_pt(shape.top_left.x + shape.size.width - 1, y); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// Rust's unit testing framework is currently a bit under-developed so we'll use |
| 170 | +// this little helper. |
| 171 | +pub fn check_strs(actual: &str, expected: &str) -> bool |
| 172 | +{ |
| 173 | + if actual != expected |
| 174 | + { |
| 175 | + io::stderr().write_line(fmt!("Found:\n%s\nbut expected\n%s", actual, expected)); |
| 176 | + return false; |
| 177 | + } |
| 178 | + return true; |
| 179 | +} |
| 180 | + |
| 181 | + |
| 182 | +fn test_ascii_art_ctor() |
| 183 | +{ |
| 184 | + let art = AsciiArt(3, 3, '*'); |
| 185 | + assert check_strs(art.to_str(), "...\n...\n..."); |
| 186 | +} |
| 187 | + |
| 188 | + |
| 189 | +fn test_add_pt() |
| 190 | +{ |
| 191 | + let art = AsciiArt(3, 3, '*'); |
| 192 | + art.add_pt(0, 0); |
| 193 | + art.add_pt(0, -10); |
| 194 | + art.add_pt(1, 2); |
| 195 | + assert check_strs(art.to_str(), "*..\n...\n.*."); |
| 196 | +} |
| 197 | + |
| 198 | + |
| 199 | +fn test_shapes() |
| 200 | +{ |
| 201 | + let art = AsciiArt(4, 4, '*'); |
| 202 | + art.add_rect(Rect {top_left: Point {x: 0, y: 0}, size: Size {width: 4, height: 4}}); |
| 203 | + art.add_point(Point {x: 2, y: 2}); |
| 204 | + assert check_strs(art.to_str(), "****\n*..*\n*.**\n****"); |
| 205 | +} |
| 206 | + |
| 207 | +fn main() { |
| 208 | + test_ascii_art_ctor(); |
| 209 | + test_add_pt(); |
| 210 | + test_shapes(); |
| 211 | +} |
| 212 | + |
0 commit comments