Skip to content

Commit c43426e

Browse files
killerswanbrson
authored andcommitted
CamelCase and demode json.rs and deque.rs
1 parent 4db474e commit c43426e

File tree

2 files changed

+96
-78
lines changed

2 files changed

+96
-78
lines changed

src/libstd/deque.rs

Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
//! A deque. Untested as of yet. Likely buggy
2+
#[forbid(deprecated_mode)];
3+
#[forbid(deprecated_pattern)];
4+
#[forbid(non_camel_case_types)];
25

36
use option::{Some, None};
47
use dvec::DVec;
8+
use core::cmp::{Eq};
59

610
trait Deque<T> {
711
fn size() -> uint;
@@ -24,8 +28,9 @@ fn create<T: Copy>() -> Deque<T> {
2428
* Grow is only called on full elts, so nelts is also len(elts), unlike
2529
* elsewhere.
2630
*/
27-
fn grow<T: Copy>(nelts: uint, lo: uint, -elts: ~[mut Cell<T>]) ->
28-
~[mut Cell<T>] {
31+
fn grow<T: Copy>(nelts: uint, lo: uint, +elts: ~[mut Cell<T>])
32+
-> ~[mut Cell<T>] {
33+
let elts = move elts;
2934
assert (nelts == vec::len(elts));
3035
let mut rv = ~[mut];
3136

@@ -40,8 +45,8 @@ fn create<T: Copy>() -> Deque<T> {
4045

4146
move rv
4247
}
43-
fn get<T: Copy>(elts: DVec<Cell<T>>, i: uint) -> T {
44-
match elts.get_elt(i) { Some(t) => t, _ => fail }
48+
fn get<T: Copy>(elts: &DVec<Cell<T>>, i: uint) -> T {
49+
match (*elts).get_elt(i) { Some(t) => t, _ => fail }
4550
}
4651

4752
type Repr<T> = {mut nelts: uint,
@@ -79,7 +84,7 @@ fn create<T: Copy>() -> Deque<T> {
7984
* that we don't keep anyone's refcount up unexpectedly.
8085
*/
8186
fn pop_front() -> T {
82-
let t: T = get(self.elts, self.lo);
87+
let t: T = get(&self.elts, self.lo);
8388
self.elts.set_elt(self.lo, None);
8489
self.lo = (self.lo + 1u) % self.elts.len();
8590
self.nelts -= 1u;
@@ -89,16 +94,16 @@ fn create<T: Copy>() -> Deque<T> {
8994
if self.hi == 0u {
9095
self.hi = self.elts.len() - 1u;
9196
} else { self.hi -= 1u; }
92-
let t: T = get(self.elts, self.hi);
97+
let t: T = get(&self.elts, self.hi);
9398
self.elts.set_elt(self.hi, None);
9499
self.nelts -= 1u;
95100
return t;
96101
}
97-
fn peek_front() -> T { return get(self.elts, self.lo); }
98-
fn peek_back() -> T { return get(self.elts, self.hi - 1u); }
102+
fn peek_front() -> T { return get(&self.elts, self.lo); }
103+
fn peek_back() -> T { return get(&self.elts, self.hi - 1u); }
99104
fn get(i: int) -> T {
100105
let idx = (self.lo + (i as uint)) % self.elts.len();
101-
return get(self.elts, idx);
106+
return get(&self.elts, idx);
102107
}
103108
}
104109

@@ -160,7 +165,13 @@ mod tests {
160165
assert (d.get(3) == 4);
161166
}
162167

163-
fn test_boxes(a: @int, b: @int, c: @int, d: @int) {
168+
#[test]
169+
fn test_boxes() {
170+
let a: @int = @5;
171+
let b: @int = @72;
172+
let c: @int = @64;
173+
let d: @int = @175;
174+
164175
let deq: deque::Deque<@int> = deque::create::<@int>();
165176
assert (deq.size() == 0u);
166177
deq.add_front(a);
@@ -190,11 +201,7 @@ mod tests {
190201
assert (deq.get(3) == d);
191202
}
192203

193-
type EqFn<T> = fn@(T, T) -> bool;
194-
195-
fn test_parameterized<T: Copy Owned>(
196-
e: EqFn<T>, a: T, b: T, c: T, d: T) {
197-
204+
fn test_parameterized<T: Copy Eq Owned>(+a: T, +b: T, +c: T, +d: T) {
198205
let deq: deque::Deque<T> = deque::create::<T>();
199206
assert (deq.size() == 0u);
200207
deq.add_front(a);
@@ -203,12 +210,12 @@ mod tests {
203210
assert (deq.size() == 3u);
204211
deq.add_back(d);
205212
assert (deq.size() == 4u);
206-
assert (e(deq.peek_front(), b));
207-
assert (e(deq.peek_back(), d));
208-
assert (e(deq.pop_front(), b));
209-
assert (e(deq.pop_back(), d));
210-
assert (e(deq.pop_back(), c));
211-
assert (e(deq.pop_back(), a));
213+
assert deq.peek_front() == b;
214+
assert deq.peek_back() == d;
215+
assert deq.pop_front() == b;
216+
assert deq.pop_back() == d;
217+
assert deq.pop_back() == c;
218+
assert deq.pop_back() == a;
212219
assert (deq.size() == 0u);
213220
deq.add_back(c);
214221
assert (deq.size() == 1u);
@@ -218,10 +225,10 @@ mod tests {
218225
assert (deq.size() == 3u);
219226
deq.add_front(a);
220227
assert (deq.size() == 4u);
221-
assert (e(deq.get(0), a));
222-
assert (e(deq.get(1), b));
223-
assert (e(deq.get(2), c));
224-
assert (e(deq.get(3), d));
228+
assert deq.get(0) == a;
229+
assert deq.get(1) == b;
230+
assert deq.get(2) == c;
231+
assert deq.get(3) == d;
225232
}
226233

227234
enum Taggy { One(int), Two(int, int), Three(int, int, int), }
@@ -232,78 +239,86 @@ mod tests {
232239

233240
type RecCy = {x: int, y: int, t: Taggy};
234241

235-
#[test]
236-
fn test() {
237-
fn inteq(&&a: int, &&b: int) -> bool { return a == b; }
238-
fn intboxeq(&&a: @int, &&b: @int) -> bool { return a == b; }
239-
fn taggyeq(a: Taggy, b: Taggy) -> bool {
240-
match a {
241-
One(a1) => match b {
242+
impl Taggy : Eq {
243+
pure fn eq(other: Taggy) -> bool {
244+
match self {
245+
One(a1) => match other {
242246
One(b1) => return a1 == b1,
243247
_ => return false
244248
},
245-
Two(a1, a2) => match b {
249+
Two(a1, a2) => match other {
246250
Two(b1, b2) => return a1 == b1 && a2 == b2,
247251
_ => return false
248252
},
249-
Three(a1, a2, a3) => match b {
253+
Three(a1, a2, a3) => match other {
250254
Three(b1, b2, b3) => return a1 == b1 && a2 == b2 && a3 == b3,
251255
_ => return false
252256
}
253257
}
254258
}
255-
fn taggypareq<T>(a: Taggypar<T>, b: Taggypar<T>) -> bool {
256-
match a {
257-
Onepar::<T>(a1) => match b {
258-
Onepar::<T>(b1) => return a1 == b1,
259-
_ => return false
260-
},
261-
Twopar::<T>(a1, a2) => match b {
262-
Twopar::<T>(b1, b2) => return a1 == b1 && a2 == b2,
263-
_ => return false
264-
},
265-
Threepar::<T>(a1, a2, a3) => match b {
266-
Threepar::<T>(b1, b2, b3) => {
267-
return a1 == b1 && a2 == b2 && a3 == b3
268-
}
269-
_ => return false
270-
}
271-
}
259+
pure fn ne(other: Taggy) -> bool { !self.eq(other) }
260+
}
261+
262+
impl Taggypar<int> : Eq {
263+
//let eq4: EqFn<Taggypar<int>> = |x,y| taggypareq::<int>(x, y);
264+
pure fn eq(other: Taggypar<int>) -> bool {
265+
match self {
266+
Onepar::<int>(a1) => match other {
267+
Onepar::<int>(b1) => return a1 == b1,
268+
_ => return false
269+
},
270+
Twopar::<int>(a1, a2) => match other {
271+
Twopar::<int>(b1, b2) => return a1 == b1 && a2 == b2,
272+
_ => return false
273+
},
274+
Threepar::<int>(a1, a2, a3) => match other {
275+
Threepar::<int>(b1, b2, b3) => {
276+
return a1 == b1 && a2 == b2 && a3 == b3
277+
}
278+
_ => return false
279+
}
280+
}
272281
}
273-
fn reccyeq(a: RecCy, b: RecCy) -> bool {
274-
return a.x == b.x && a.y == b.y && taggyeq(a.t, b.t);
282+
pure fn ne(other: Taggypar<int>) -> bool { !self.eq(other) }
283+
}
284+
285+
impl RecCy : Eq {
286+
pure fn eq(other: RecCy) -> bool {
287+
return self.x == other.x && self.y == other.y && self.t == other.t;
275288
}
276-
debug!("*** test boxes");
277-
test_boxes(@5, @72, @64, @175);
278-
debug!("*** end test boxes");
279-
debug!("test parameterized: int");
280-
let eq1: EqFn<int> = inteq;
281-
test_parameterized::<int>(eq1, 5, 72, 64, 175);
282-
debug!("*** test parameterized: @int");
283-
let eq2: EqFn<@int> = intboxeq;
284-
test_parameterized::<@int>(eq2, @5, @72, @64, @175);
285-
debug!("*** end test parameterized @int");
286-
debug!("test parameterized: taggy");
287-
let eq3: EqFn<Taggy> = taggyeq;
288-
test_parameterized::<Taggy>(eq3, One(1), Two(1, 2), Three(1, 2, 3),
289+
pure fn ne(other: RecCy) -> bool { !self.eq(other) }
290+
}
291+
292+
#[test]
293+
fn test_param_int() {
294+
test_parameterized::<int>(5, 72, 64, 175);
295+
}
296+
297+
#[test]
298+
fn test_param_at_int() {
299+
test_parameterized::<@int>(@5, @72, @64, @175);
300+
}
301+
302+
#[test]
303+
fn test_param_taggy() {
304+
test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3),
289305
Two(17, 42));
306+
}
290307

291-
debug!("*** test parameterized: taggypar<int>");
292-
let eq4: EqFn<Taggypar<int>> = |x,y| taggypareq::<int>(x, y);
293-
test_parameterized::<Taggypar<int>>(eq4, Onepar::<int>(1),
308+
#[test]
309+
fn test_param_taggypar() {
310+
test_parameterized::<Taggypar<int>>(Onepar::<int>(1),
294311
Twopar::<int>(1, 2),
295312
Threepar::<int>(1, 2, 3),
296313
Twopar::<int>(17, 42));
297-
debug!("*** end test parameterized: taggypar::<int>");
314+
}
298315

299-
debug!("*** test parameterized: reccy");
316+
#[test]
317+
fn test_param_reccy() {
300318
let reccy1: RecCy = {x: 1, y: 2, t: One(1)};
301319
let reccy2: RecCy = {x: 345, y: 2, t: Two(1, 2)};
302320
let reccy3: RecCy = {x: 1, y: 777, t: Three(1, 2, 3)};
303321
let reccy4: RecCy = {x: 19, y: 252, t: Two(17, 42)};
304-
let eq5: EqFn<RecCy> = reccyeq;
305-
test_parameterized::<RecCy>(eq5, reccy1, reccy2, reccy3, reccy4);
306-
debug!("*** end test parameterized: reccy");
307-
debug!("*** done");
322+
test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4);
308323
}
309324
}

src/libstd/json.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// Rust JSON serialization library
22
// Copyright (c) 2011 Google Inc.
3+
#[forbid(deprecated_mode)];
4+
#[forbid(deprecated_pattern)];
5+
#[forbid(non_camel_case_types)];
36

47
//! json serialization
58
@@ -174,7 +177,7 @@ fn to_writer_pretty(wr: io::Writer, j: Json, indent: uint) {
174177
}
175178
}
176179

177-
fn escape_str(s: ~str) -> ~str {
180+
fn escape_str(s: &str) -> ~str {
178181
let mut escaped = ~"\"";
179182
do str::chars_iter(s) |c| {
180183
match c {
@@ -574,7 +577,7 @@ fn from_reader(rdr: io::Reader) -> Result<Json, Error> {
574577
}
575578

576579
/// Deserializes a json value from a string
577-
fn from_str(s: ~str) -> Result<Json, Error> {
580+
fn from_str(s: &str) -> Result<Json, Error> {
578581
io::with_str_reader(s, from_reader)
579582
}
580583

@@ -828,7 +831,7 @@ impl Error: to_str::ToStr {
828831

829832
#[cfg(test)]
830833
mod tests {
831-
fn mk_dict(items: ~[(~str, Json)]) -> Json {
834+
fn mk_dict(items: &[(~str, Json)]) -> Json {
832835
let d = map::str_hash();
833836

834837
do vec::iter(items) |item| {

0 commit comments

Comments
 (0)