rustlings/exercises/19_smart_pointers/cow1.rs

70 lines
2.3 KiB
Rust
Raw Normal View History

2024-06-28 20:07:56 -04:00
// This exercise explores the `Cow` (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.
2022-07-23 17:57:03 -04:00
use std::borrow::Cow;
2024-06-28 20:07:56 -04:00
fn abs_all(input: &mut Cow<[i32]>) {
for ind in 0..input.len() {
let value = input[ind];
if value < 0 {
2022-07-23 17:57:03 -04:00
// Clones into a vector if not already owned.
2024-06-28 20:07:56 -04:00
input.to_mut()[ind] = -value;
2022-07-23 17:57:03 -04:00
}
}
}
fn main() {
// You can optionally experiment here.
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
2024-06-28 20:07:56 -04:00
fn reference_mutation() {
// Clone occurs because `input` needs to be mutated.
2024-06-28 20:07:56 -04:00
let vec = vec![-1, 0, 1];
let mut input = Cow::from(&vec);
abs_all(&mut input);
assert!(matches!(input, Cow::Owned(_)));
2022-07-23 17:57:03 -04:00
}
#[test]
2024-06-28 20:07:56 -04:00
fn reference_no_mutation() {
// No clone occurs because `input` doesn't need to be mutated.
2024-06-28 20:07:56 -04:00
let vec = vec![0, 1, 2];
let mut input = Cow::from(&vec);
abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!()));
2022-07-23 17:57:03 -04:00
}
#[test]
2024-06-28 20:07:56 -04:00
fn owned_no_mutation() {
// We can also pass `vec` without `&` so `Cow` owns it directly. In this
2024-07-07 11:03:00 -04:00
// case, no mutation occurs (all numbers are already absolute) and thus
// also no clone. But the result is still owned because it was never
// borrowed or mutated.
2024-06-28 20:07:56 -04:00
let vec = vec![0, 1, 2];
let mut input = Cow::from(vec);
abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!()));
}
#[test]
2024-06-28 20:07:56 -04:00
fn owned_mutation() {
2024-07-07 11:03:00 -04:00
// Of course this is also the case if a mutation does occur (not all
// numbers are absolute). In this case, the call to `to_mut()` in the
// `abs_all` function returns a reference to the same data as before.
2024-06-28 20:07:56 -04:00
let vec = vec![-1, 0, 1];
let mut input = Cow::from(vec);
abs_all(&mut input);
// TODO: Replace `todo!()` with `Cow::Owned(_)` or `Cow::Borrowed(_)`.
assert!(matches!(input, todo!()));
2022-07-23 17:57:03 -04:00
}
}