Skip to content

Commit 2df06a0

Browse files
committed
---
yaml --- r: 127417 b: refs/heads/auto c: 351cc4f h: refs/heads/master i: 127415: dd98971 v: v3
1 parent 9f7bd6a commit 2df06a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1333
-318
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: 0c158b4fbfcec7d6f18859661047dff2109fdfe4
16+
refs/heads/auto: 351cc4fc99b7579cf53c5dbeb35cfb224ced03d8
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/doc/guide.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,86 @@ building our guessing game, but we need to know how to do one last thing first:
14321432
get input from the keyboard. You can't have a guessing game without the ability
14331433
to guess!
14341434

1435+
# Strings
1436+
1437+
Strings are an important concept for any programmer to master. Rust's string
1438+
handling system is a bit different than in other languages, due to its systems
1439+
focus. Any time you have a data structure of variable size, things can get
1440+
tricky, and strings are a re-sizable data structure. That said, Rust's strings
1441+
also work differently than in some other systems languages, such as C.
1442+
1443+
Let's dig into the details. A **string** is a sequence of unicode scalar values
1444+
encoded as a stream of UTF-8 bytes. All strings are guaranteed to be
1445+
validly-encoded UTF-8 sequences. Additionally, strings are not null-terminated
1446+
and can contain null bytes.
1447+
1448+
Rust has two main types of strings: `&str` and `String`.
1449+
1450+
The first kind is a `&str`. This is pronounced a 'string slice.' String literals
1451+
are of the type `&str`:
1452+
1453+
```{rust}
1454+
let string = "Hello there.";
1455+
```
1456+
1457+
This string is statically allocated, meaning that it's saved inside our
1458+
compiled program, and exists for the entire duration it runs. The `string`
1459+
binding is a reference to this statically allocated string. String slices
1460+
have a fixed size, and cannot be mutated.
1461+
1462+
A `String`, on the other hand, is an in-memory string. This string is
1463+
growable, and is also guaranteed to be UTF-8.
1464+
1465+
```{rust}
1466+
let mut s = "Hello".to_string();
1467+
println!("{}", s);
1468+
1469+
s.push_str(", world.");
1470+
println!("{}", s);
1471+
```
1472+
1473+
You can coerce a `String` into a `&str` with the `as_slice()` method:
1474+
1475+
```{rust}
1476+
fn takes_slice(slice: &str) {
1477+
println!("Got: {}", slice);
1478+
}
1479+
1480+
fn main() {
1481+
let s = "Hello".to_string();
1482+
takes_slice(s.as_slice());
1483+
}
1484+
```
1485+
1486+
To compare a String to a constant string, prefer `as_slice()`...
1487+
1488+
```{rust}
1489+
fn compare(string: String) {
1490+
if string.as_slice() == "Hello" {
1491+
println!("yes");
1492+
}
1493+
}
1494+
```
1495+
1496+
... over `to_string()`:
1497+
1498+
```{rust}
1499+
fn compare(string: String) {
1500+
if string == "Hello".to_string() {
1501+
println!("yes");
1502+
}
1503+
}
1504+
```
1505+
1506+
Converting a `String` to a `&str` is cheap, but converting the `&str` to a
1507+
`String` involves allocating memory. No reason to do that unless you have to!
1508+
1509+
That's the basics of strings in Rust! They're probably a bit more complicated
1510+
than you are used to, if you come from a scripting language, but when the
1511+
low-level details matter, they really matter. Just remember that `String`s
1512+
allocate memory and control their data, while `&str`s are a reference to
1513+
another string, and you'll be all set.
1514+
14351515
# Standard Input
14361516

14371517
Getting input from the keyboard is pretty easy, but uses some things
@@ -3614,6 +3694,94 @@ guide](http://doc.rust-lang.org/guide-pointers.html#rc-and-arc).
36143694

36153695
# Patterns
36163696

3697+
# Method Syntax
3698+
3699+
Functions are great, but if you want to call a bunch of them on some data, it
3700+
can be awkward. Consider this code:
3701+
3702+
```{rust,ignore}
3703+
baz(bar(foo(x)));
3704+
```
3705+
3706+
We would read this left-to right, and so we see 'baz bar foo.' But this isn't the
3707+
order that the functions would get called in, that's inside-out: 'foo bar baz.'
3708+
Wouldn't it be nice if we could do this instead?
3709+
3710+
```{rust,ignore}
3711+
x.foo().bar().baz();
3712+
```
3713+
3714+
Luckily, as you may have guessed with the leading question, you can! Rust provides
3715+
the ability to use this **method call syntax** via the `impl` keyword.
3716+
3717+
Here's how it works:
3718+
3719+
```
3720+
struct Circle {
3721+
x: f64,
3722+
y: f64,
3723+
radius: f64,
3724+
}
3725+
3726+
impl Circle {
3727+
fn area(&self) -> f64 {
3728+
std::f64::consts::PI * (self.radius * self.radius)
3729+
}
3730+
}
3731+
3732+
fn main() {
3733+
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
3734+
println!("{}", c.area());
3735+
}
3736+
```
3737+
3738+
This will print `12.566371`.
3739+
3740+
We've made a struct that represents a circle. We then write an `impl` block,
3741+
and inside it, define a method, `area`. Methods take a special first
3742+
parameter, `&self`. There are three variants: `self`, `&self`, and `&mut self`.
3743+
You can think of this first parameter as being the `x` in `x.foo()`. The three
3744+
variants correspond to the three kinds of thing `x` could be: `self` if it's
3745+
just a value on the stack, `&self` if it's a reference, and `&mut self` if it's
3746+
a mutable reference. We should default to using `&self`, as it's the most
3747+
common.
3748+
3749+
Finally, as you may remember, the value of the area of a circle is `π*r²`.
3750+
Because we took the `&self` parameter to `area`, we can use it just like any
3751+
other parameter. Because we know it's a `Circle`, we can access the `radius`
3752+
just like we would with any other struct. An import of π and some
3753+
multiplications later, and we have our area.
3754+
3755+
You can also define methods that do not take a `self` parameter. Here's a
3756+
pattern that's very common in Rust code:
3757+
3758+
```
3759+
struct Circle {
3760+
x: f64,
3761+
y: f64,
3762+
radius: f64,
3763+
}
3764+
3765+
impl Circle {
3766+
fn new(x: f64, y: f64, radius: f64) -> Circle {
3767+
Circle {
3768+
x: x,
3769+
y: y,
3770+
radius: radius,
3771+
}
3772+
}
3773+
}
3774+
3775+
fn main() {
3776+
let c = Circle::new(0.0, 0.0, 2.0);
3777+
}
3778+
```
3779+
3780+
This **static method** builds a new `Circle` for us. Note that static methods
3781+
are called with the `Struct::method()` syntax, rather than the `ref.method()`
3782+
syntax.
3783+
3784+
36173785
# Closures
36183786

36193787
So far, we've made lots of functions in Rust. But we've given them all names.

branches/auto/src/libcollections/ringbuf.rs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ impl<'a, T> ExactSize<&'a mut T> for MutItems<'a, T> {}
403403
fn grow<T>(nelts: uint, loptr: &mut uint, elts: &mut Vec<Option<T>>) {
404404
assert_eq!(nelts, elts.len());
405405
let lo = *loptr;
406-
let newlen = nelts * 2;
407-
elts.reserve(newlen);
406+
elts.reserve(nelts * 2);
407+
let newlen = elts.capacity();
408408

409409
/* fill with None */
410-
for _ in range(elts.len(), elts.capacity()) {
410+
for _ in range(elts.len(), newlen) {
411411
elts.push(None);
412412
}
413413

@@ -750,6 +750,47 @@ mod tests {
750750
assert_eq!(d.len(), 1);
751751
}
752752

753+
#[test]
754+
fn test_with_capacity_non_power_two() {
755+
let mut d3 = RingBuf::with_capacity(3);
756+
d3.push(1i);
757+
758+
// X = None, | = lo
759+
// [|1, X, X]
760+
assert_eq!(d3.pop_front(), Some(1));
761+
// [X, |X, X]
762+
assert_eq!(d3.front(), None);
763+
764+
// [X, |3, X]
765+
d3.push(3);
766+
// [X, |3, 6]
767+
d3.push(6);
768+
// [X, X, |6]
769+
assert_eq!(d3.pop_front(), Some(3));
770+
771+
// Pushing the lo past half way point to trigger
772+
// the 'B' scenario for growth
773+
// [9, X, |6]
774+
d3.push(9);
775+
// [9, 12, |6]
776+
d3.push(12);
777+
778+
d3.push(15);
779+
// There used to be a bug here about how the
780+
// RingBuf made growth assumptions about the
781+
// underlying Vec which didn't hold and lead
782+
// to corruption.
783+
// (Vec grows to next power of two)
784+
//good- [9, 12, 15, X, X, X, X, |6]
785+
//bug- [15, 12, X, X, X, |6, X, X]
786+
assert_eq!(d3.pop_front(), Some(6));
787+
788+
// Which leads us to the following state which
789+
// would be a failure case.
790+
//bug- [15, 12, X, X, X, X, |X, X]
791+
assert_eq!(d3.front(), Some(&9));
792+
}
793+
753794
#[test]
754795
fn test_reserve_exact() {
755796
let mut d = RingBuf::new();

branches/auto/src/libcore/num/f32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl Float for f32 {
293293
#[inline]
294294
fn frac_pi_8() -> f32 { consts::FRAC_PI_8 }
295295

296-
/// 1 .0/ pi
296+
/// 1.0 / pi
297297
#[inline]
298298
fn frac_1_pi() -> f32 { consts::FRAC_1_PI }
299299

branches/auto/src/liblibc/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,18 +1142,16 @@ pub mod types {
11421142
pub mod os {
11431143
pub mod common {
11441144
pub mod posix01 {
1145-
use types::os::arch::c95::{c_short, time_t, suseconds_t,
1146-
c_long};
1145+
use types::os::arch::c95::{c_short, time_t, c_long};
11471146
use types::os::arch::extra::{int64, time64_t};
11481147
use types::os::arch::posix88::{dev_t, ino_t};
1149-
use types::os::arch::posix88::mode_t;
11501148

11511149
// pub Note: this is the struct called stat64 in win32. Not stat,
11521150
// nor stati64.
11531151
pub struct stat {
11541152
pub st_dev: dev_t,
11551153
pub st_ino: ino_t,
1156-
pub st_mode: mode_t,
1154+
pub st_mode: u16,
11571155
pub st_nlink: c_short,
11581156
pub st_uid: c_short,
11591157
pub st_gid: c_short,
@@ -1171,8 +1169,8 @@ pub mod types {
11711169
}
11721170

11731171
pub struct timeval {
1174-
pub tv_sec: time_t,
1175-
pub tv_usec: suseconds_t,
1172+
pub tv_sec: c_long,
1173+
pub tv_usec: c_long,
11761174
}
11771175

11781176
pub struct timespec {
@@ -1186,7 +1184,7 @@ pub mod types {
11861184
pub mod bsd44 {
11871185
use types::os::arch::c95::{c_char, c_int, c_uint, size_t};
11881186

1189-
pub type SOCKET = c_uint;
1187+
pub type SOCKET = uint;
11901188
pub type socklen_t = c_int;
11911189
pub type sa_family_t = u16;
11921190
pub type in_port_t = u16;
@@ -1197,6 +1195,7 @@ pub mod types {
11971195
}
11981196
pub struct sockaddr_storage {
11991197
pub ss_family: sa_family_t,
1198+
pub __ss_pad1: [u8, ..6],
12001199
pub __ss_align: i64,
12011200
pub __ss_pad2: [u8, ..112],
12021201
}
@@ -1293,12 +1292,9 @@ pub mod types {
12931292
pub mod posix88 {
12941293
pub type off_t = i32;
12951294
pub type dev_t = u32;
1296-
pub type ino_t = i16;
1295+
pub type ino_t = u16;
12971296

1298-
#[cfg(target_arch = "x86")]
1299-
pub type pid_t = i32;
1300-
#[cfg(target_arch = "x86_64")]
1301-
pub type pid_t = i64;
1297+
pub type pid_t = u32;
13021298

13031299
pub type useconds_t = u32;
13041300
pub type mode_t = u16;
@@ -1415,7 +1411,7 @@ pub mod types {
14151411
pub dwPageSize: DWORD,
14161412
pub lpMinimumApplicationAddress: LPVOID,
14171413
pub lpMaximumApplicationAddress: LPVOID,
1418-
pub dwActiveProcessorMask: DWORD,
1414+
pub dwActiveProcessorMask: uint,
14191415
pub dwNumberOfProcessors: DWORD,
14201416
pub dwProcessorType: DWORD,
14211417
pub dwAllocationGranularity: DWORD,
@@ -1950,7 +1946,7 @@ pub mod consts {
19501946
}
19511947
pub mod extra {
19521948
use types::os::arch::c95::c_int;
1953-
use types::os::arch::extra::{WORD, DWORD, BOOL};
1949+
use types::os::arch::extra::{WORD, DWORD, BOOL, HANDLE};
19541950

19551951
pub static TRUE : BOOL = 1;
19561952
pub static FALSE : BOOL = 0;
@@ -1979,7 +1975,7 @@ pub mod consts {
19791975
pub static ERROR_IO_PENDING: c_int = 997;
19801976
pub static ERROR_FILE_INVALID : c_int = 1006;
19811977
pub static ERROR_NOT_FOUND: c_int = 1168;
1982-
pub static INVALID_HANDLE_VALUE : c_int = -1;
1978+
pub static INVALID_HANDLE_VALUE: HANDLE = -1 as HANDLE;
19831979

19841980
pub static DELETE : DWORD = 0x00010000;
19851981
pub static READ_CONTROL : DWORD = 0x00020000;

branches/auto/src/libnative/io/c_win32.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub static ENABLE_PROCESSED_INPUT: libc::DWORD = 0x1;
2828
pub static ENABLE_QUICK_EDIT_MODE: libc::DWORD = 0x40;
2929

3030
#[repr(C)]
31+
#[cfg(target_arch = "x86")]
3132
pub struct WSADATA {
3233
pub wVersion: libc::WORD,
3334
pub wHighVersion: libc::WORD,
@@ -37,6 +38,17 @@ pub struct WSADATA {
3738
pub iMaxUdpDg: u16,
3839
pub lpVendorInfo: *mut u8,
3940
}
41+
#[repr(C)]
42+
#[cfg(target_arch = "x86_64")]
43+
pub struct WSADATA {
44+
pub wVersion: libc::WORD,
45+
pub wHighVersion: libc::WORD,
46+
pub iMaxSockets: u16,
47+
pub iMaxUdpDg: u16,
48+
pub lpVendorInfo: *mut u8,
49+
pub szDescription: [u8, ..WSADESCRIPTION_LEN + 1],
50+
pub szSystemStatus: [u8, ..WSASYS_STATUS_LEN + 1],
51+
}
4052

4153
pub type LPWSADATA = *mut WSADATA;
4254

0 commit comments

Comments
 (0)