Skip to content

Commit 1f2005f

Browse files
committed
fixes
1 parent 2c9cc73 commit 1f2005f

File tree

5 files changed

+48
-373
lines changed

5 files changed

+48
-373
lines changed

Chapter01/testing/src/lib.rs

+1-68
Original file line numberDiff line numberDiff line change
@@ -139,52 +139,6 @@ impl<T> List<T> where T: Sized + Clone {
139139
.value
140140
})
141141
}
142-
143-
144-
///
145-
/// Splits off and returns `n` nodes as a `List<T>`.
146-
///
147-
/// # Arguments
148-
///
149-
/// `n: usize` - The number of elements after which to split the list.
150-
///
151-
/// # Panics
152-
///
153-
/// Panics when:
154-
/// - The list is empty
155-
/// - `n` is larger than the length
156-
///
157-
/// # Example
158-
///
159-
/// ```
160-
/// # use testing::List;
161-
///
162-
/// let mut list = List::new_empty();
163-
/// list.append(12);
164-
/// list.append(11);
165-
/// list.append(10);
166-
/// let mut list2 = list.split(1);
167-
/// assert_eq!(list2.pop(), Some(12));
168-
/// assert_eq!(list.pop(), Some(11));
169-
/// assert_eq!(list.pop(), Some(10));
170-
/// ```
171-
///
172-
pub fn split(&mut self, n: usize) -> List<T> {
173-
174-
// Don't do this in real life. Use Results, Options, or anything that
175-
// doesn't just kill the program
176-
if self.length == 0 || n >= self.length - 1 {
177-
panic!("That's not working");
178-
}
179-
180-
let mut n = n;
181-
let mut new_list = List::new_empty();
182-
while n > 0 {
183-
new_list.append(self.pop().unwrap());
184-
n -= 1;
185-
}
186-
new_list
187-
}
188142
}
189143

190144
impl <T>Drop for List<T> where T: Clone + Sized {
@@ -225,7 +179,7 @@ mod tests {
225179
list.append(1);
226180
list.append(1);
227181
list.append(1);
228-
list.append(1);
182+
list.append(1);
229183
list.append(1);
230184
assert_eq!(list.length, 5);
231185
}
@@ -248,25 +202,4 @@ mod tests {
248202
assert_eq!(list.length, 0);
249203
assert_eq!(list.pop(), None);
250204
}
251-
252-
#[test]
253-
fn test_list_split() {
254-
let mut list = List::new_empty();
255-
list.append(1);
256-
list.append(1);
257-
list.append(1);
258-
list.append(1);
259-
list.append(1);
260-
assert_eq!(list.length, 5);
261-
let list2 = list.split(3);
262-
assert_eq!(list.length, 2);
263-
assert_eq!(list2.length, 3);
264-
}
265-
266-
#[test]
267-
#[should_panic]
268-
fn test_list_split_panics() {
269-
let mut list: List<i32> = List::new_empty();
270-
let _ = list.split(3);
271-
}
272205
}

Chapter01/traits/src/lib.rs

+40-38
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1-
use std::io::{Write, Read};
1+
use std::io::{Read, Write};
22
// Structs
33

44
///
55
/// Configuration for our application
6-
///
6+
///
77
pub struct Config {
8-
values: Vec<(String, String)>
8+
values: Vec<(String, String)>,
99
}
1010

1111
///
1212
/// A service for managing a configuration
13-
///
14-
pub struct KeyValueConfigService {
15-
16-
}
13+
///
14+
pub struct KeyValueConfigService {}
1715

1816
// Traits
1917

2018
///
21-
/// Provides a get() function to return valuse associated with
19+
/// Provides a get() function to return values associated with
2220
/// the specified key.
23-
///
21+
///
2422
pub trait ValueGetter {
2523
fn get(&self, s: &str) -> Option<String>;
2624
}
2725

2826
///
2927
/// Write a config
30-
///
28+
///
3129
pub trait ConfigWriter {
3230
fn write(&self, config: Config, to: &mut impl Write) -> std::io::Result<()>;
3331
}
3432

3533
///
3634
/// Read a config
37-
///
35+
///
3836
pub trait ConfigReader {
3937
fn read(&self, from: &mut impl Read) -> std::io::Result<Config>;
4038
}
@@ -43,16 +41,13 @@ pub trait ConfigReader {
4341

4442
impl Config {
4543
pub fn new(values: Vec<(String, String)>) -> Config {
46-
Config {
47-
values: values
48-
}
44+
Config { values: values }
4945
}
5046
}
5147

5248
impl KeyValueConfigService {
5349
pub fn new() -> KeyValueConfigService {
54-
KeyValueConfigService {
55-
}
50+
KeyValueConfigService {}
5651
}
5752
}
5853

@@ -72,34 +67,35 @@ impl ConfigReader for KeyValueConfigService {
7267

7368
// chain iterators together and collect the results
7469
let values: Vec<(String, String)> = buffer
75-
.split_terminator("\n") // split
70+
.split_terminator("\n") // split
7671
.map(|line| line.trim()) // remove whitespace
77-
.filter(|line| { // filter invalid lines
78-
let pos = line.find("=")
79-
.unwrap_or(0);
72+
.filter(|line| {
73+
// filter invalid lines
74+
let pos = line.find("=").unwrap_or(0);
8075
pos > 0 && pos < line.len() - 1
8176
})
82-
.map(|line| { // create a tuple from a line
77+
.map(|line| {
78+
// create a tuple from a line
8379
let parts = line.split("=").collect::<Vec<&str>>();
8480
(parts[0].to_string(), parts[1].to_string())
8581
})
86-
.collect(); // transform it into a vector
82+
.collect(); // transform it into a vector
8783
Ok(Config::new(values))
8884
}
8985
}
9086

9187
impl ValueGetter for Config {
9288
fn get(&self, s: &str) -> Option<String> {
93-
self.values.iter()
94-
.find_map(|tuple| if &tuple.0 == s {
95-
Some(tuple.1.clone())
96-
} else {
97-
None
98-
})
89+
self.values.iter().find_map(|tuple| {
90+
if &tuple.0 == s {
91+
Some(tuple.1.clone())
92+
} else {
93+
None
94+
}
95+
})
9996
}
10097
}
10198

102-
10399
#[cfg(test)]
104100
mod tests {
105101
use super::*;
@@ -112,7 +108,6 @@ mod tests {
112108
assert_eq!(config.get("HELLO"), None);
113109
}
114110

115-
116111
#[test]
117112
fn keyvalueconfigservice_write_config() {
118113
let config = Config::new(vec![("hello".to_string(), "world".to_string())]);
@@ -121,20 +116,27 @@ mod tests {
121116
let mut target = vec![];
122117
assert!(service.write(config, &mut target).is_ok());
123118

124-
assert_eq!(String::from_utf8(target).unwrap(), "hello=world\n".to_string());
119+
assert_eq!(
120+
String::from_utf8(target).unwrap(),
121+
"hello=world\n".to_string()
122+
);
125123
}
126124

127-
#[test]
125+
#[test]
128126
fn keyvalueconfigservice_read_config() {
129-
130127
let service = KeyValueConfigService::new();
131128
let readable = &format!("{}\n{}", "hello=world", "a=b").into_bytes();
132-
133-
let config = service.read(&mut Cursor::new(readable))
129+
130+
let config = service
131+
.read(&mut Cursor::new(readable))
134132
.expect("Couldn't read from the vector");
135133

136-
assert_eq!(config.values, vec![
134+
assert_eq!(
135+
config.values,
136+
vec![
137137
("hello".to_string(), "world".to_string()),
138-
("a".to_string(), "b".to_string())]);
138+
("a".to_string(), "b".to_string())
139+
]
140+
);
139141
}
140-
}
142+
}

Chapter02/mut-sharing-ownership/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![feature(test)]
22

3-
//pub mod list;
4-
53
#[cfg(test)]
64
mod tests {
75
extern crate test;

0 commit comments

Comments
 (0)