Skip to content

Commit ec8f148

Browse files
Erick Tryzelaarerickt
authored andcommitted
---
yaml --- r: 36819 b: refs/heads/try2 c: a0ef334 h: refs/heads/master i: 36817: 77f2792 36815: 2fc8d65 v: v3
1 parent 449fc76 commit ec8f148

File tree

4 files changed

+104
-87
lines changed

4 files changed

+104
-87
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: eb8fd119c65c67f3b1b8268cc7341c22d39b7b61
55
refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 938058b0040e3c482e10b78eeef7afb941b2b64e
8+
refs/heads/try2: a0ef334179714e0c3f1a3c7276543a0305db2c95
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
1010
refs/heads/dist-snap: 22efa39382d41b084fde1719df7ae8ce5697d8c9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596

branches/try2/src/libcore/option.rs

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,6 @@ pub pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T {
9696
}
9797
}
9898

99-
pub pure fn expect<T>(opt: Option<T>, reason: ~str) -> T {
100-
/*!
101-
* Gets the value out of an option without copying, printing a
102-
* specified message on failure.
103-
*
104-
* # Failure
105-
*
106-
* Fails if the value equals `none`
107-
*/
108-
if opt.is_some() { move option::unwrap(move opt) }
109-
else { fail reason }
110-
}
111-
11299
pub pure fn map<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> {
113100
//! Maps a `some` value by reference from one type to another
114101
@@ -235,35 +222,46 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T {
235222
unwrap(util::replace(opt, None))
236223
}
237224

238-
pub pure fn unwrap_expect<T>(opt: Option<T>, reason: &str) -> T {
225+
pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T {
239226
//! As unwrap, but with a specified failure message.
240-
if opt.is_none() { fail reason.to_owned(); }
241-
unwrap(move opt)
227+
match move opt {
228+
Some(move val) => val,
229+
None => fail reason.to_owned(),
230+
}
242231
}
243232

244-
// Some of these should change to be &Option<T>, some should not. See below.
245233
impl<T> Option<T> {
246234
/// Returns true if the option equals `none`
247-
pure fn is_none() -> bool { is_none(&self) }
235+
#[inline(always)]
236+
pure fn is_none(&self) -> bool { is_none(self) }
237+
248238
/// Returns true if the option contains some value
249-
pure fn is_some() -> bool { is_some(&self) }
250-
}
239+
#[inline(always)]
240+
pure fn is_some(&self) -> bool { is_some(self) }
251241

252-
impl<T> &Option<T> {
253242
/**
254243
* Update an optional value by optionally running its content by reference
255244
* through a function that returns an option.
256245
*/
257-
pure fn chain_ref<U>(f: fn(x: &T) -> Option<U>) -> Option<U> {
246+
#[inline(always)]
247+
pure fn chain_ref<U>(&self, f: fn(x: &T) -> Option<U>) -> Option<U> {
258248
chain_ref(self, f)
259249
}
250+
251+
/// Maps a `some` value from one type to another by reference
252+
#[inline(always)]
253+
pure fn map<U>(&self, f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
254+
260255
/// Applies a function to the contained value or returns a default
261-
pure fn map_default<U>(def: U, f: fn(x: &T) -> U) -> U
262-
{ map_default(self, move def, f) }
256+
#[inline(always)]
257+
pure fn map_default<U>(&self, def: U, f: fn(x: &T) -> U) -> U {
258+
map_default(self, move def, f)
259+
}
260+
263261
/// Performs an operation on the contained value by reference
264-
pure fn iter(f: fn(x: &T)) { iter(self, f) }
265-
/// Maps a `some` value from one type to another by reference
266-
pure fn map<U>(f: fn(x: &T) -> U) -> Option<U> { map(self, f) }
262+
#[inline(always)]
263+
pure fn iter(&self, f: fn(x: &T)) { iter(self, f) }
264+
267265
/**
268266
Gets an immutable reference to the value inside an option.
269267
@@ -278,7 +276,29 @@ impl<T> &Option<T> {
278276
Instead, prefer to use pattern matching and handle the `None`
279277
case explicitly.
280278
*/
281-
pure fn get_ref() -> &self/T { get_ref(self) }
279+
#[inline(always)]
280+
pure fn get_ref(&self) -> &self/T { get_ref(self) }
281+
282+
/**
283+
* Gets the value out of an option without copying.
284+
*
285+
* # Failure
286+
*
287+
* Fails if the value equals `none`
288+
*/
289+
#[inline(always)]
290+
pure fn unwrap(self) -> T { unwrap(self) }
291+
292+
/**
293+
* Gets the value out of an option, printing a specified message on
294+
* failure
295+
*
296+
* # Failure
297+
*
298+
* Fails if the value equals `none`
299+
*/
300+
#[inline(always)]
301+
pure fn expect(self, reason: &str) -> T { expect(self, reason) }
282302
}
283303

284304
impl<T: Copy> Option<T> {
@@ -296,19 +316,17 @@ impl<T: Copy> Option<T> {
296316
Instead, prefer to use pattern matching and handle the `None`
297317
case explicitly.
298318
*/
299-
pure fn get() -> T { get(self) }
300-
pure fn get_default(def: T) -> T { get_default(self, def) }
301-
/**
302-
* Gets the value out of an option, printing a specified message on
303-
* failure
304-
*
305-
* # Failure
306-
*
307-
* Fails if the value equals `none`
308-
*/
309-
pure fn expect(reason: ~str) -> T { expect(self, move reason) }
319+
#[inline(always)]
320+
pure fn get(self) -> T { get(self) }
321+
322+
#[inline(always)]
323+
pure fn get_default(self, def: T) -> T { get_default(self, def) }
324+
310325
/// Applies a function zero or more times until the result is none.
311-
pure fn while_some(blk: fn(v: T) -> Option<T>) { while_some(self, blk) }
326+
#[inline(always)]
327+
pure fn while_some(self, blk: fn(v: T) -> Option<T>) {
328+
while_some(self, blk)
329+
}
312330
}
313331

314332
#[test]

branches/try2/src/libcore/pipes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ Fails if the sender closes the connection.
412412
*/
413413
pub fn recv<T: Owned, Tbuffer: Owned>(
414414
p: RecvPacketBuffered<T, Tbuffer>) -> T {
415-
option::unwrap_expect(try_recv(move p), "connection closed")
415+
try_recv(move p).expect("connection closed")
416416
}
417417

418418
/** Attempts to receive a message from a pipe.
@@ -1102,7 +1102,7 @@ impl<T: Owned> PortSet<T> : GenericPort<T> {
11021102
}
11031103

11041104
fn recv() -> T {
1105-
option::unwrap_expect(self.try_recv(), "port_set: endpoints closed")
1105+
self.try_recv().expect("port_set: endpoints closed")
11061106
}
11071107

11081108
}

branches/try2/src/libcore/result.rs

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
114114
* ok(parse_bytes(buf))
115115
* }
116116
*/
117-
pub fn chain<T, U, V>(res: Result<T, V>, op: fn(t: T)
117+
pub pure fn chain<T, U, V>(res: Result<T, V>, op: fn(T)
118118
-> Result<U, V>) -> Result<U, V> {
119119
match move res {
120120
Ok(move t) => op(move t),
@@ -130,7 +130,7 @@ pub fn chain<T, U, V>(res: Result<T, V>, op: fn(t: T)
130130
* immediately returned. This function can be used to pass through a
131131
* successful result while handling an error.
132132
*/
133-
pub fn chain_err<T, U, V>(
133+
pub pure fn chain_err<T, U, V>(
134134
res: Result<T, V>,
135135
op: fn(t: V) -> Result<T, U>)
136136
-> Result<T, U> {
@@ -154,7 +154,7 @@ pub fn chain_err<T, U, V>(
154154
* print_buf(buf)
155155
* }
156156
*/
157-
pub fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) {
157+
pub pure fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) {
158158
match *res {
159159
Ok(ref t) => f(t),
160160
Err(_) => ()
@@ -169,7 +169,7 @@ pub fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) {
169169
* This function can be used to pass through a successful result while
170170
* handling an error.
171171
*/
172-
pub fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) {
172+
pub pure fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) {
173173
match *res {
174174
Ok(_) => (),
175175
Err(ref e) => f(e)
@@ -190,7 +190,7 @@ pub fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) {
190190
* parse_bytes(buf)
191191
* }
192192
*/
193-
pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U)
193+
pub pure fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U)
194194
-> Result<U, E> {
195195
match *res {
196196
Ok(ref t) => Ok(op(t)),
@@ -206,7 +206,7 @@ pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U)
206206
* is immediately returned. This function can be used to pass through a
207207
* successful result while handling an error.
208208
*/
209-
pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F)
209+
pub pure fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F)
210210
-> Result<T, F> {
211211
match *res {
212212
Ok(copy t) => Ok(t),
@@ -215,58 +215,55 @@ pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F)
215215
}
216216

217217
impl<T, E> Result<T, E> {
218+
#[inline(always)]
218219
pure fn get_ref(&self) -> &self/T { get_ref(self) }
219220

220-
pure fn is_ok() -> bool { is_ok(&self) }
221+
#[inline(always)]
222+
pure fn is_ok(&self) -> bool { is_ok(self) }
221223

222-
pure fn is_err() -> bool { is_err(&self) }
224+
#[inline(always)]
225+
pure fn is_err(&self) -> bool { is_err(self) }
223226

224-
pure fn iter(f: fn(&T)) {
225-
match self {
226-
Ok(ref t) => f(t),
227-
Err(_) => ()
228-
}
227+
#[inline(always)]
228+
pure fn iter(&self, f: fn(&T)) { iter(self, f) }
229+
230+
#[inline(always)]
231+
pure fn iter_err(&self, f: fn(&E)) { iter_err(self, f) }
232+
233+
#[inline(always)]
234+
pure fn unwrap(self) -> T { unwrap(self) }
235+
236+
#[inline(always)]
237+
pure fn unwrap_err(self) -> T { unwrap(self) }
238+
239+
#[inline(always)]
240+
pure fn chain<U>(self, op: fn(T) -> Result<U,E>) -> Result<U,E> {
241+
chain(self, op)
229242
}
230243

231-
fn iter_err(f: fn(&E)) {
232-
match self {
233-
Ok(_) => (),
234-
Err(ref e) => f(e)
235-
}
244+
#[inline(always)]
245+
pure fn chain_err<F>(self, op: fn(E) -> Result<T,F>) -> Result<T,F> {
246+
chain_err(self, op)
236247
}
237248
}
238249

239250
impl<T: Copy, E> Result<T, E> {
240-
pure fn get() -> T { get(&self) }
251+
#[inline(always)]
252+
pure fn get(&self) -> T { get(self) }
241253

242-
fn map_err<F:Copy>(op: fn(&E) -> F) -> Result<T,F> {
243-
match self {
244-
Ok(copy t) => Ok(t),
245-
Err(ref e) => Err(op(e))
246-
}
254+
#[inline(always)]
255+
pure fn map_err<F:Copy>(&self, op: fn(&E) -> F) -> Result<T,F> {
256+
map_err(self, op)
247257
}
248258
}
249259

250260
impl<T, E: Copy> Result<T, E> {
251-
pure fn get_err() -> E { get_err(&self) }
252-
253-
fn map<U:Copy>(op: fn(&T) -> U) -> Result<U,E> {
254-
match self {
255-
Ok(ref t) => Ok(op(t)),
256-
Err(copy e) => Err(e)
257-
}
258-
}
259-
}
260-
261-
impl<T: Copy, E: Copy> Result<T, E> {
262-
fn chain<U:Copy>(op: fn(t: T) -> Result<U,E>) -> Result<U,E> {
263-
// XXX: Bad copy
264-
chain(copy self, op)
265-
}
261+
#[inline(always)]
262+
pure fn get_err(&self) -> E { get_err(self) }
266263

267-
fn chain_err<F:Copy>(op: fn(t: E) -> Result<T,F>) -> Result<T,F> {
268-
// XXX: Bad copy
269-
chain_err(copy self, op)
264+
#[inline(always)]
265+
pure fn map<U:Copy>(&self, op: fn(&T) -> U) -> Result<U,E> {
266+
map(self, op)
270267
}
271268
}
272269

@@ -360,15 +357,17 @@ pub fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T],
360357
}
361358

362359
/// Unwraps a result, assuming it is an `ok(T)`
363-
pub fn unwrap<T, U>(res: Result<T, U>) -> T {
360+
#[inline(always)]
361+
pub pure fn unwrap<T, U>(res: Result<T, U>) -> T {
364362
match move res {
365363
Ok(move t) => move t,
366364
Err(_) => fail ~"unwrap called on an err result"
367365
}
368366
}
369367

370368
/// Unwraps a result, assuming it is an `err(U)`
371-
pub fn unwrap_err<T, U>(res: Result<T, U>) -> U {
369+
#[inline(always)]
370+
pub pure fn unwrap_err<T, U>(res: Result<T, U>) -> U {
372371
match move res {
373372
Err(move u) => move u,
374373
Ok(_) => fail ~"unwrap called on an ok result"

0 commit comments

Comments
 (0)