@@ -46,7 +46,13 @@ pure fn map<T, U>(opt: option<T>, f: fn(T) -> U) -> option<U> {
46
46
match opt { some( x) => some ( f ( x) ) , none => none }
47
47
}
48
48
49
- pure fn map_consume < T , U > ( -opt : option < T > , f : fn ( -T ) -> U ) -> option < U > {
49
+ pure fn map_ref < T , U > ( opt : & option < T > , f : fn ( x : & T ) -> U ) -> option < U > {
50
+ //! Maps a `some` value by reference from one type to another
51
+
52
+ match * opt { some( ref x) => some ( f ( x) ) , none => none }
53
+ }
54
+
55
+ pure fn map_consume < T , U > ( +opt : option < T > , f : fn ( +T ) -> U ) -> option < U > {
50
56
/*!
51
57
* As `map`, but consumes the option and gives `f` ownership to avoid
52
58
* copying.
@@ -63,6 +69,16 @@ pure fn chain<T, U>(opt: option<T>, f: fn(T) -> option<U>) -> option<U> {
63
69
match opt { some( x) => f ( x) , none => none }
64
70
}
65
71
72
+ pure fn chain_ref < T , U > ( opt : & option < T > ,
73
+ f : fn ( x : & T ) -> option < U > ) -> option < U > {
74
+ /*!
75
+ * Update an optional value by optionally running its content by reference
76
+ * through a function that returns an option.
77
+ */
78
+
79
+ match * opt { some( ref x) => f ( x) , none => none }
80
+ }
81
+
66
82
#[ inline( always) ]
67
83
pure fn while_some < T > ( +x : option < T > , blk : fn ( +T ) -> option < T > ) {
68
84
//! Applies a function zero or more times until the result is none.
@@ -97,14 +113,28 @@ pure fn map_default<T, U>(opt: option<T>, +def: U, f: fn(T) -> U) -> U {
97
113
match opt { none => def, some( t) => f ( t) }
98
114
}
99
115
116
+ // This should replace map_default.
117
+ pure fn map_default_ref < T , U > ( opt : & option < T > , +def : U ,
118
+ f : fn ( x : & T ) -> U ) -> U {
119
+ //! Applies a function to the contained value or returns a default
120
+
121
+ match * opt { none => def, some( ref t) => f ( t) }
122
+ }
123
+
124
+ // This should change to by-copy mode; use iter_ref below for by reference
100
125
pure fn iter < T > ( opt : option < T > , f : fn ( T ) ) {
101
126
//! Performs an operation on the contained value or does nothing
102
127
103
128
match opt { none => ( ) , some( t) => f ( t) }
104
129
}
105
130
131
+ pure fn iter_ref < T > ( opt : & option < T > , f : fn ( x : & T ) ) {
132
+ //! Performs an operation on the contained value by reference
133
+ match * opt { none => ( ) , some( ref t) => f ( t) }
134
+ }
135
+
106
136
#[ inline( always) ]
107
- pure fn unwrap<T >( - opt: option<T >) -> T {
137
+ pure fn unwrap < T > ( + opt : option < T > ) -> T {
108
138
/*!
109
139
* Moves a value out of an option type and returns it.
110
140
*
@@ -130,12 +160,13 @@ fn swap_unwrap<T>(opt: &mut option<T>) -> T {
130
160
unwrap ( util:: replace ( opt, none) )
131
161
}
132
162
133
- pure fn unwrap_expect<T >( - opt: option<T >, reason: & str) -> T {
163
+ pure fn unwrap_expect < T > ( + opt : option < T > , reason : & str ) -> T {
134
164
//! As unwrap, but with a specified failure message.
135
165
if opt. is_none ( ) { fail reason. to_unique ( ) ; }
136
166
unwrap ( opt)
137
167
}
138
168
169
+ // Some of these should change to be &option<T>, some should not. See below.
139
170
impl < T > option < T > {
140
171
/**
141
172
* Update an optional value by optionally running its content through a
@@ -155,6 +186,23 @@ impl<T> option<T> {
155
186
pure fn map < U > ( f : fn ( T ) -> U ) -> option < U > { map ( self , f) }
156
187
}
157
188
189
+ impl < T > & option < T > {
190
+ /**
191
+ * Update an optional value by optionally running its content by reference
192
+ * through a function that returns an option.
193
+ */
194
+ pure fn chain_ref < U > ( f : fn ( x : & T ) -> option < U > ) -> option < U > {
195
+ chain_ref ( self , f)
196
+ }
197
+ /// Applies a function to the contained value or returns a default
198
+ pure fn map_default_ref < U > ( +def : U , f : fn ( x : & T ) -> U ) -> U
199
+ { map_default_ref ( self , def, f) }
200
+ /// Performs an operation on the contained value by reference
201
+ pure fn iter_ref ( f : fn ( x : & T ) ) { iter_ref ( self , f) }
202
+ /// Maps a `some` value from one type to another by reference
203
+ pure fn map_ref < U > ( f : fn ( x : & T ) -> U ) -> option < U > { map_ref ( self , f) }
204
+ }
205
+
158
206
impl < T : copy > option < T > {
159
207
/**
160
208
* Gets the value out of an option
0 commit comments