summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorliv <mokou@fastmail.com>2023-02-15 14:16:22 +0100
committerGitHub <noreply@github.com>2023-02-15 14:16:22 +0100
commit216d08d0135ccf1b5d3fe014b53cc150bb1775e4 (patch)
tree01c68560191ea0160476daea78f154f8786364d5 /exercises
parent149e0c8ac2a18336b0e4ed0da4a55919150117fa (diff)
parentbbdc5c60395ce7e126bcd6f8babacdf6a76ea2d9 (diff)
Merge pull request #1244 from black-puppydog/cow-rework
refactor(cow1): replace main with tests
Diffstat (limited to 'exercises')
-rw-r--r--exercises/smart_pointers/cow1.rs66
1 files changed, 46 insertions, 20 deletions
diff --git a/exercises/smart_pointers/cow1.rs b/exercises/smart_pointers/cow1.rs
index 5fba251..885138a 100644
--- a/exercises/smart_pointers/cow1.rs
+++ b/exercises/smart_pointers/cow1.rs
@@ -4,6 +4,9 @@
// 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.
// 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
@@ -20,29 +23,52 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
input
}
-fn main() {
- // No clone occurs because `input` doesn't need to be mutated.
- let slice = [0, 1, 2];
- let mut input = Cow::from(&slice[..]);
- match abs_all(&mut input) {
- Cow::Borrowed(_) => println!("I borrowed the slice!"),
- _ => panic!("expected borrowed value"),
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn reference_mutation() -> Result<(), &'static str> {
+ // 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.
- let slice = [-1, 0, 1];
- let mut input = Cow::from(&slice[..]);
- match abs_all(&mut input) {
- Cow::Owned(_) => println!("I modified the slice and now own it!"),
- _ => panic!("expected owned value"),
+ #[test]
+ fn reference_no_mutation() -> Result<(), &'static str> {
+ // No clone occurs because `input` doesn't need to be mutated.
+ let slice = [0, 1, 2];
+ let mut input = Cow::from(&slice[..]);
+ match abs_all(&mut input) {
+ // TODO
+ }
}
- // No clone occurs because `input` is already owned.
- let slice = vec![-1, 0, 1];
- let mut input = Cow::from(slice);
- match abs_all(&mut input) {
- // TODO
- Cow::Borrowed(_) => println!("I own this slice!"),
- _ => panic!("expected borrowed value"),
+ #[test]
+ fn owned_no_mutation() -> Result<(), &'static str> {
+ // We can also pass `slice` without `&` so Cow owns it directly.
+ // In this case no mutation occurs and thus also no clone,
+ // but the result is still owned because it always was.
+ let slice = vec![0, 1, 2];
+ 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
+ }
}
}