summaryrefslogtreecommitdiff
path: root/exercises/smart_pointers/cow1.rs
diff options
context:
space:
mode:
authorEmmanuel Roullit <eroullit@github.com>2023-02-25 17:11:43 +0100
committerGitHub <noreply@github.com>2023-02-25 17:11:43 +0100
commitfcadbfc70d578e4a993711d5a7f1737aebd6b3ce (patch)
tree897142c1904755a43aefce56695c0b8186381e69 /exercises/smart_pointers/cow1.rs
parentb653d4848a52701d2240f130ab74c158dd5d7069 (diff)
parent701b4bef51b50d1fd3bb7fbfe3cc274f2bbdcb0c (diff)
Merge branch 'rust-lang:main' into codespaces
Diffstat (limited to 'exercises/smart_pointers/cow1.rs')
-rw-r--r--exercises/smart_pointers/cow1.rs74
1 files changed, 74 insertions, 0 deletions
diff --git a/exercises/smart_pointers/cow1.rs b/exercises/smart_pointers/cow1.rs
new file mode 100644
index 0000000..885138a
--- /dev/null
+++ b/exercises/smart_pointers/cow1.rs
@@ -0,0 +1,74 @@
+// cow1.rs
+
+// This exercise explores the Cow, or Clone-On-Write type.
+// 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
+
+use std::borrow::Cow;
+
+fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
+ for i in 0..input.len() {
+ let v = input[i];
+ if v < 0 {
+ // Clones into a vector if not already owned.
+ input.to_mut()[i] = -v;
+ }
+ }
+ input
+}
+
+#[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"),
+ }
+ }
+
+ #[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
+ }
+ }
+
+ #[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
+ }
+ }
+}