44// Cow is a clone-on-write smart pointer.
55// It can enclose and provide immutable access to borrowed data, and clone the data lazily when mutation or ownership is required.
66// The type is designed to work with general borrowed data via the Borrow trait.
7+ //
8+ // This exercise is meant to show you what to expect when passing data to Cow.
9+ // Fix the unit tests by checking for Cow::Owned(_) and Cow::Borrowed(_) at the TODO markers.
710
811// I AM NOT DONE
912
@@ -20,29 +23,52 @@ fn abs_all<'a, 'b>(input: &'a mut Cow<'b, [i32]>) -> &'a mut Cow<'b, [i32]> {
2023 input
2124}
2225
23- fn main ( ) {
24- // No clone occurs because `input` doesn't need to be mutated.
25- let slice = [ 0 , 1 , 2 ] ;
26- let mut input = Cow :: from ( & slice[ ..] ) ;
27- match abs_all ( & mut input) {
28- Cow :: Borrowed ( _) => println ! ( "I borrowed the slice!" ) ,
29- _ => panic ! ( "expected borrowed value" ) ,
26+ #[ cfg( test) ]
27+ mod tests {
28+ use super :: * ;
29+
30+ #[ test]
31+ fn reference_mutation ( ) -> Result < ( ) , & ' static str > {
32+ // Clone occurs because `input` needs to be mutated.
33+ let slice = [ -1 , 0 , 1 ] ;
34+ let mut input = Cow :: from ( & slice[ ..] ) ;
35+ match abs_all ( & mut input) {
36+ Cow :: Owned ( _) => Ok ( ( ) ) ,
37+ _ => Err ( "Expected owned value" ) ,
38+ }
3039 }
3140
32- // Clone occurs because `input` needs to be mutated.
33- let slice = [ -1 , 0 , 1 ] ;
34- let mut input = Cow :: from ( & slice[ ..] ) ;
35- match abs_all ( & mut input) {
36- Cow :: Owned ( _) => println ! ( "I modified the slice and now own it!" ) ,
37- _ => panic ! ( "expected owned value" ) ,
41+ #[ test]
42+ fn reference_no_mutation ( ) -> Result < ( ) , & ' static str > {
43+ // No clone occurs because `input` doesn't need to be mutated.
44+ let slice = [ 0 , 1 , 2 ] ;
45+ let mut input = Cow :: from ( & slice[ ..] ) ;
46+ match abs_all ( & mut input) {
47+ // TODO
48+ }
3849 }
3950
40- // No clone occurs because `input` is already owned.
41- let slice = vec ! [ -1 , 0 , 1 ] ;
42- let mut input = Cow :: from ( slice) ;
43- match abs_all ( & mut input) {
44- // TODO
45- Cow :: Borrowed ( _) => println ! ( "I own this slice!" ) ,
46- _ => panic ! ( "expected borrowed value" ) ,
51+ #[ test]
52+ fn owned_no_mutation ( ) -> Result < ( ) , & ' static str > {
53+ // We can also pass `slice` without `&` so Cow owns it directly.
54+ // In this case no mutation occurs and thus also no clone,
55+ // but the result is still owned because it always was.
56+ let slice = vec ! [ 0 , 1 , 2 ] ;
57+ let mut input = Cow :: from ( slice) ;
58+ match abs_all ( & mut input) {
59+ // TODO
60+ }
61+ }
62+
63+ #[ test]
64+ fn owned_mutation ( ) -> Result < ( ) , & ' static str > {
65+ // Of course this is also the case if a mutation does occur.
66+ // In this case the call to `to_mut()` returns a reference to
67+ // the same data as before.
68+ let slice = vec ! [ -1 , 0 , 1 ] ;
69+ let mut input = Cow :: from ( slice) ;
70+ match abs_all ( & mut input) {
71+ // TODO
72+ }
4773 }
4874}
0 commit comments