Skip to content

Commit 776b39b

Browse files
committed
---
yaml --- r: 134981 b: refs/heads/snap-stage3 c: 2550243 h: refs/heads/master i: 134979: c03d7f1 v: v3
1 parent 30d7662 commit 776b39b

File tree

9 files changed

+209
-13
lines changed

9 files changed

+209
-13
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 7eb9337dace88c5ded431aa7507f06d50619131b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 6473909a1b1ff5c435d75d4df844c4b08dafcee9
4+
refs/heads/snap-stage3: 2550243b4183783e463fbb0bc141ab77f2898e64
55
refs/heads/try: 14378ea357c06c23607ca61ade44f60a7a64a1c7
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libcore/cmp.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
4040
#![stable]
4141

42-
use option::{Option, Some};
42+
use option::{Option, Some, None};
4343

4444
/// Trait for values that can be compared for equality and inequality.
4545
///
@@ -268,6 +268,32 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
268268
if v1 > v2 { v1 } else { v2 }
269269
}
270270

271+
/// Compare and return the minimum of two values if there is one.
272+
///
273+
/// Returns the first argument if the comparison determines them to be equal.
274+
#[inline]
275+
#[experimental]
276+
pub fn partial_min<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
277+
match v1.partial_cmp(&v2) {
278+
Some(Less) | Some(Equal) => Some(v1),
279+
Some(Greater) => Some(v2),
280+
None => None
281+
}
282+
}
283+
284+
/// Compare and return the maximum of two values if there is one.
285+
///
286+
/// Returns the first argument if the comparison determines them to be equal.
287+
#[inline]
288+
#[experimental]
289+
pub fn partial_max<T: PartialOrd>(v1: T, v2: T) -> Option<T> {
290+
match v1.partial_cmp(&v2) {
291+
Some(Less) => Some(v2),
292+
Some(Equal) | Some(Greater) => Some(v1),
293+
None => None
294+
}
295+
}
296+
271297
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
272298
mod impls {
273299
use cmp::{PartialOrd, Ord, PartialEq, Eq, Ordering,

branches/snap-stage3/src/libcore/default.rs

Lines changed: 108 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,104 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! The `Default` trait for types which may have meaningful default values
11+
//! The `Default` trait for types which may have meaningful default values.
12+
//!
13+
//! Sometimes, you want to fall back to some kind of default value, and
14+
//! don't particularly care what it is. This comes up often with `struct`s
15+
//! that define a set of options:
16+
//!
17+
//! ```
18+
//! struct SomeOptions {
19+
//! foo: int,
20+
//! bar: f32,
21+
//! }
22+
//! ```
23+
//!
24+
//! How can we define some default values? You can use `Default`:
25+
//!
26+
//! ```
27+
//! use std::default::Default;
28+
//!
29+
//! #[deriving(Default)]
30+
//! struct SomeOptions {
31+
//! foo: int,
32+
//! bar: f32,
33+
//! }
34+
//!
35+
//!
36+
//! fn main() {
37+
//! let options: SomeOptions = Default::default();
38+
//! }
39+
//! ```
40+
//!
41+
//! Now, you get all of the default values. Rust implements `Default` for various primitives types.
42+
//! If you have your own type, you need to implement `Default` yourself:
43+
//!
44+
//! ```
45+
//! use std::default::Default;
46+
//!
47+
//! enum Kind {
48+
//! A,
49+
//! B,
50+
//! C,
51+
//! }
52+
//!
53+
//! impl Default for Kind {
54+
//! fn default() -> Kind { A }
55+
//! }
56+
//!
57+
//! #[deriving(Default)]
58+
//! struct SomeOptions {
59+
//! foo: int,
60+
//! bar: f32,
61+
//! baz: Kind,
62+
//! }
63+
//!
64+
//!
65+
//! fn main() {
66+
//! let options: SomeOptions = Default::default();
67+
//! }
68+
//! ```
69+
//!
70+
//! If you want to override a particular option, but still retain the other defaults:
71+
//!
72+
//! ```
73+
//! # use std::default::Default;
74+
//! # #[deriving(Default)]
75+
//! # struct SomeOptions {
76+
//! # foo: int,
77+
//! # bar: f32,
78+
//! # }
79+
//! fn main() {
80+
//! let options = SomeOptions { foo: 42, ..Default::default() };
81+
//! }
82+
//! ```
1283
1384
#![stable]
1485

1586
/// A trait that types which have a useful default value should implement.
87+
///
88+
/// A struct can derive default implementations of `Default` for basic types using
89+
/// `#[deriving(Default)]`.
90+
///
91+
/// # Examples
92+
///
93+
/// ```
94+
/// #[deriving(Default)]
95+
/// struct SomeOptions {
96+
/// foo: int,
97+
/// bar: f32,
98+
/// }
99+
/// ```
16100
pub trait Default {
17-
/// Return the "default value" for a type.
101+
/// Returns the "default value" for a type.
18102
///
19-
/// # Example
103+
/// Default values are often some kind of initial value, identity value, or anything else that
104+
/// may make sense as a default.
105+
///
106+
/// # Examples
107+
///
108+
/// Using built-in default values:
20109
///
21110
/// ```
22111
/// use std::default::Default;
@@ -25,6 +114,22 @@ pub trait Default {
25114
/// let (x, y): (Option<String>, f64) = Default::default();
26115
/// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
27116
/// ```
117+
///
118+
/// Making your own:
119+
///
120+
/// ```
121+
/// use std::default::Default;
122+
///
123+
/// enum Kind {
124+
/// A,
125+
/// B,
126+
/// C,
127+
/// }
128+
///
129+
/// impl Default for Kind {
130+
/// fn default() -> Kind { A }
131+
/// }
132+
/// ```
28133
fn default() -> Self;
29134
}
30135

branches/snap-stage3/src/libcore/iter.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,8 +811,7 @@ impl<A: Add<A, A> + Zero, T: Iterator<A>> AdditiveIterator<A> for T {
811811
}
812812
}
813813

814-
/// A trait for iterators over elements whose elements can be multiplied
815-
/// together.
814+
/// A trait for iterators over elements which can be multiplied together.
816815
pub trait MultiplicativeIterator<A> {
817816
/// Iterates over the entire iterator, multiplying all the elements
818817
///
@@ -840,7 +839,6 @@ impl<A: Mul<A, A> + One, T: Iterator<A>> MultiplicativeIterator<A> for T {
840839
}
841840

842841
/// A trait for iterators over elements which can be compared to one another.
843-
/// The type of each element must ascribe to the `PartialOrd` trait.
844842
pub trait OrdIterator<A> {
845843
/// Consumes the entire iterator to return the maximum element.
846844
///

branches/snap-stage3/src/libcoretest/cmp.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use core::cmp::lexical_ordering;
12+
use core::cmp::{ partial_min, partial_max };
1213

1314
#[test]
1415
fn test_int_totalord() {
@@ -56,6 +57,72 @@ fn test_lexical_ordering() {
5657
}
5758
}
5859

60+
#[test]
61+
fn test_partial_min() {
62+
use core::f64::NAN;
63+
let data_integer = [
64+
// a, b, result
65+
(0i, 0i, Some(0i)),
66+
(1i, 0i, Some(0i)),
67+
(0i, 1i, Some(0i)),
68+
(-1i, 0i, Some(-1i)),
69+
(0i, -1i, Some(-1i))
70+
];
71+
72+
let data_float = [
73+
// a, b, result
74+
(0.0f64, 0.0f64, Some(0.0f64)),
75+
(1.0f64, 0.0f64, Some(0.0f64)),
76+
(0.0f64, 1.0f64, Some(0.0f64)),
77+
(-1.0f64, 0.0f64, Some(-1.0f64)),
78+
(0.0f64, -1.0f64, Some(-1.0f64)),
79+
(NAN, NAN, None),
80+
(NAN, 1.0f64, None),
81+
(1.0f64, NAN, None)
82+
];
83+
84+
for &(a, b, result) in data_integer.iter() {
85+
assert!(partial_min(a, b) == result);
86+
}
87+
88+
for &(a, b, result) in data_float.iter() {
89+
assert!(partial_min(a, b) == result);
90+
}
91+
}
92+
93+
#[test]
94+
fn test_partial_max() {
95+
use core::f64::NAN;
96+
let data_integer = [
97+
// a, b, result
98+
(0i, 0i, Some(0i)),
99+
(1i, 0i, Some(1i)),
100+
(0i, 1i, Some(1i)),
101+
(-1i, 0i, Some(0i)),
102+
(0i, -1i, Some(0i))
103+
];
104+
105+
let data_float = [
106+
// a, b, result
107+
(0.0f64, 0.0f64, Some(0.0f64)),
108+
(1.0f64, 0.0f64, Some(1.0f64)),
109+
(0.0f64, 1.0f64, Some(1.0f64)),
110+
(-1.0f64, 0.0f64, Some(0.0f64)),
111+
(0.0f64, -1.0f64, Some(0.0f64)),
112+
(NAN, NAN, None),
113+
(NAN, 1.0f64, None),
114+
(1.0f64, NAN, None)
115+
];
116+
117+
for &(a, b, result) in data_integer.iter() {
118+
assert!(partial_max(a, b) == result);
119+
}
120+
121+
for &(a, b, result) in data_float.iter() {
122+
assert!(partial_max(a, b) == result);
123+
}
124+
}
125+
59126
#[test]
60127
fn test_user_defined_eq() {
61128
// Our type.

branches/snap-stage3/src/liblibc/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4635,7 +4635,7 @@ pub mod funcs {
46354635
option_len: socklen_t) -> c_int;
46364636
pub fn recv(socket: c_int, buf: *mut c_void, len: size_t,
46374637
flags: c_int) -> ssize_t;
4638-
pub fn send(socket: c_int, buf: *mut c_void, len: size_t,
4638+
pub fn send(socket: c_int, buf: *const c_void, len: size_t,
46394639
flags: c_int) -> ssize_t;
46404640
pub fn recvfrom(socket: c_int, buf: *mut c_void, len: size_t,
46414641
flags: c_int, addr: *mut sockaddr,
@@ -4675,7 +4675,7 @@ pub mod funcs {
46754675
pub fn closesocket(socket: SOCKET) -> c_int;
46764676
pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int,
46774677
flags: c_int) -> c_int;
4678-
pub fn send(socket: SOCKET, buf: *mut c_void, len: c_int,
4678+
pub fn send(socket: SOCKET, buf: *const c_void, len: c_int,
46794679
flags: c_int) -> c_int;
46804680
pub fn recvfrom(socket: SOCKET, buf: *mut c_void, len: c_int,
46814681
flags: c_int, addr: *mut sockaddr,

branches/snap-stage3/src/libnative/io/file_unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ impl rtio::RtioFileStream for CFile {
303303
self.flush().and_then(|()| self.fd.fsync())
304304
}
305305
fn datasync(&mut self) -> IoResult<()> {
306-
self.flush().and_then(|()| self.fd.fsync())
306+
self.flush().and_then(|()| self.fd.datasync())
307307
}
308308
fn truncate(&mut self, offset: i64) -> IoResult<()> {
309309
self.flush().and_then(|()| self.fd.truncate(offset))

branches/snap-stage3/src/libnative/io/net.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl rtio::RtioTcpStream for TcpStream {
337337
let dowrite = |nb: bool, buf: *const u8, len: uint| unsafe {
338338
let flags = if nb {c::MSG_DONTWAIT} else {0};
339339
libc::send(fd,
340-
buf as *mut libc::c_void,
340+
buf as *const _,
341341
len as wrlen,
342342
flags) as i64
343343
};

branches/snap-stage3/src/libnative/io/pipe_unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl rtio::RtioPipe for UnixStream {
173173
let dowrite = |nb: bool, buf: *const u8, len: uint| unsafe {
174174
let flags = if nb {c::MSG_DONTWAIT} else {0};
175175
libc::send(fd,
176-
buf as *mut libc::c_void,
176+
buf as *const _,
177177
len as libc::size_t,
178178
flags) as i64
179179
};

0 commit comments

Comments
 (0)