refactor(cow1): replace main with tests
Following the discussion in #1195 this is the best I could come up with. The issue for me (and apparently a few other learners) was that the code needed to complete the exercise was not _missing_, but was rather there but wrong. In the end, what made the difference between this exercise and others (for me) was that in this exercise I was supposed to learn what to *expect* of an output. So I think it makes sense here to let the learner modify the tests and not the code itself. Fixes #1195 Signed-off-by: Daan Wynen <black.puppydog@gmx.de> # Conflicts: # info.toml
This commit is contained in:
parent
149e0c8ac2
commit
bbdc5c6039
|
@ -4,6 +4,9 @@
|
||||||
// Cow is a clone-on-write smart pointer.
|
// Cow is a clone-on-write smart pointer.
|
||||||
// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
|
// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
|
||||||
// The type is designed to work with general borrowed data via the Borrow trait.
|
// The type is designed to work with general borrowed data via the Borrow trait.
|
||||||
|
//
|
||||||
|
// This exercise is meant to show you what to expect when passing data to Cow.
|
||||||
|
// Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the TODO markers.
|
||||||
|
|
||||||
// I AM NOT DONE
|
// I AM NOT DONE
|
||||||
|
|
||||||
|
@ -20,29 +23,52 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
|
||||||
input
|
input
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
#[cfg(test)]
|
||||||
// No clone occurs because `input` doesn't need to be mutated.
|
mod tests {
|
||||||
let slice = [0, 1, 2];
|
use super::*;
|
||||||
let mut input = Cow::from(&slice[..]);
|
|
||||||
match abs_all(&mut input) {
|
#[test]
|
||||||
Cow::Borrowed(_) => println!("I borrowed the slice!"),
|
fn reference_mutation() -> Result<(), &'static str> {
|
||||||
_ => panic!("expected borrowed value"),
|
// Clone occurs because `input` needs to be mutated.
|
||||||
|
let slice = [-1, 0, 1];
|
||||||
|
let mut input = Cow::from(&slice[..]);
|
||||||
|
match abs_all(&mut input) {
|
||||||
|
Cow::Owned(_) => Ok(()),
|
||||||
|
_ => Err("Expected owned value"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clone occurs because `input` needs to be mutated.
|
#[test]
|
||||||
let slice = [-1, 0, 1];
|
fn reference_no_mutation() -> Result<(), &'static str> {
|
||||||
let mut input = Cow::from(&slice[..]);
|
// No clone occurs because `input` doesn't need to be mutated.
|
||||||
match abs_all(&mut input) {
|
let slice = [0, 1, 2];
|
||||||
Cow::Owned(_) => println!("I modified the slice and now own it!"),
|
let mut input = Cow::from(&slice[..]);
|
||||||
_ => panic!("expected owned value"),
|
match abs_all(&mut input) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// No clone occurs because `input` is already owned.
|
#[test]
|
||||||
let slice = vec![-1, 0, 1];
|
fn owned_no_mutation() -> Result<(), &'static str> {
|
||||||
let mut input = Cow::from(slice);
|
// We can also pass `slice` without `&` so Cow owns it directly.
|
||||||
match abs_all(&mut input) {
|
// In this case no mutation occurs and thus also no clone,
|
||||||
// TODO
|
// but the result is still owned because it always was.
|
||||||
Cow::Borrowed(_) => println!("I own this slice!"),
|
let slice = vec![0, 1, 2];
|
||||||
_ => panic!("expected borrowed value"),
|
let mut input = Cow::from(slice);
|
||||||
|
match abs_all(&mut input) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn owned_mutation() -> Result<(), &'static str> {
|
||||||
|
// Of course this is also the case if a mutation does occur.
|
||||||
|
// In this case the call to `to_mut()` returns a reference to
|
||||||
|
// the same data as before.
|
||||||
|
let slice = vec![-1, 0, 1];
|
||||||
|
let mut input = Cow::from(slice);
|
||||||
|
match abs_all(&mut input) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1010,11 +1010,11 @@ https://doc.rust-lang.org/stable/book/ch16-00-concurrency.html
|
||||||
[[exercises]]
|
[[exercises]]
|
||||||
name = "cow1"
|
name = "cow1"
|
||||||
path = "exercises/smart_pointers/cow1.rs"
|
path = "exercises/smart_pointers/cow1.rs"
|
||||||
mode = "compile"
|
mode = "test"
|
||||||
hint = """
|
hint = """
|
||||||
Since the vector is already owned, the `Cow` type doesn't need to clone it.
|
If Cow already owns the data it doesn't need to clone it when to_mut() is called.
|
||||||
|
|
||||||
Checkout https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
|
Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
|
||||||
on the `Cow` type.
|
on the `Cow` type.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue