Skip to content

Commit f41d70c

Browse files
committed
---
yaml --- r: 137566 b: refs/heads/master c: d569dfe h: refs/heads/master v: v3
1 parent f1ca342 commit f41d70c

File tree

16 files changed

+216
-79
lines changed

16 files changed

+216
-79
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 98a0f9166c38d9a761a840de15a668ea001c9e9c
2+
refs/heads/master: d569dfe37eac393509351c28db01e50e0ca323c2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: cd53dac86b43e0b46f06ab265b71526242a2fc5e

trunk/src/doc/guide-macros.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ forbidden.
8787

8888
Otherwise, the invocation syntax is free-form.
8989

90-
To take as an argument a fragment of Rust code, write `$` followed by a name
90+
To take a fragment of Rust code as an argument, write `$` followed by a name
9191
(for use on the right-hand side), followed by a `:`, followed by a *fragment
9292
specifier*. The fragment specifier denotes the sort of fragment to match. The
9393
most common fragment specifiers are:

trunk/src/doc/guide.md

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5212,8 +5212,8 @@ We can check this out using a special flag to `rustc`. This code, in a file
52125212

52135213
```{rust}
52145214
fn main() {
5215-
let x = "Hello";
5216-
println!("x is: {:s}", x);
5215+
let x = 5i;
5216+
println!("x is: {}", x);
52175217
}
52185218
```
52195219

@@ -5225,32 +5225,19 @@ give us this huge result:
52255225
#![no_std]
52265226
#![feature(globs)]
52275227
#[phase(plugin, link)]
5228-
extern crate std = "std";
5229-
extern crate rt = "native";
5228+
extern crate "std" as std;
5229+
extern crate "native" as rt;
5230+
#[prelude_import]
52305231
use std::prelude::*;
52315232
fn main() {
5232-
let x = "Hello";
5233+
let x = 5i;
52335234
match (&x,) {
52345235
(__arg0,) => {
52355236
#[inline]
52365237
#[allow(dead_code)]
5237-
static __STATIC_FMTSTR: [::std::fmt::rt::Piece<'static>, ..2u] =
5238-
[::std::fmt::rt::String("x is: "),
5239-
::std::fmt::rt::Argument(::std::fmt::rt::Argument{position:
5240-
::std::fmt::rt::ArgumentNext,
5241-
format:
5242-
::std::fmt::rt::FormatSpec{fill:
5243-
' ',
5244-
align:
5245-
::std::fmt::rt::AlignUnknown,
5246-
flags:
5247-
0u,
5248-
precision:
5249-
::std::fmt::rt::CountImplied,
5250-
width:
5251-
::std::fmt::rt::CountImplied,},})];
5238+
static __STATIC_FMTSTR: [&'static str, ..1u] = ["x is: "];
52525239
let __args_vec =
5253-
&[::std::fmt::argument(::std::fmt::secret_string, __arg0)];
5240+
&[::std::fmt::argument(::std::fmt::secret_show, __arg0)];
52545241
let __args =
52555242
unsafe {
52565243
::std::fmt::Arguments::new(__STATIC_FMTSTR, __args_vec)
@@ -5261,45 +5248,16 @@ fn main() {
52615248
}
52625249
```
52635250

5264-
Intense. Here's a trimmed down version that's a bit easier to read:
5265-
5266-
```{rust,ignore}
5267-
fn main() {
5268-
let x = 5i;
5269-
match (&x,) {
5270-
(__arg0,) => {
5271-
static __STATIC_FMTSTR: =
5272-
[String("x is: "),
5273-
Argument(Argument {
5274-
position: ArgumentNext,
5275-
format: FormatSpec {
5276-
fill: ' ',
5277-
align: AlignUnknown,
5278-
flags: 0u,
5279-
precision: CountImplied,
5280-
width: CountImplied,
5281-
},
5282-
},
5283-
];
5284-
let __args_vec = &[argument(secret_string, __arg0)];
5285-
let __args = unsafe { Arguments::new(__STATIC_FMTSTR, __args_vec) };
5286-
5287-
println_args(&__args)
5288-
}
5289-
};
5290-
}
5291-
```
5292-
52935251
Whew! This isn't too terrible. You can see that we still `let x = 5i`,
52945252
but then things get a little bit hairy. Three more bindings get set: a
52955253
static format string, an argument vector, and the arguments. We then
52965254
invoke the `println_args` function with the generated arguments.
52975255

5298-
This is the code (well, the full version) that Rust actually compiles. You can
5299-
see all of the extra information that's here. We get all of the type safety and
5300-
options that it provides, but at compile time, and without needing to type all
5301-
of this out. This is how macros are powerful. Without them, you would need to
5302-
type all of this by hand to get a type checked `println`.
5256+
This is the code that Rust actually compiles. You can see all of the extra
5257+
information that's here. We get all of the type safety and options that it
5258+
provides, but at compile time, and without needing to type all of this out.
5259+
This is how macros are powerful. Without them, you would need to type all of
5260+
this by hand to get a type checked `println`.
53035261

53045262
For more on macros, please consult [the Macros Guide](guide-macros.html).
53055263
Macros are a very advanced and still slightly experimental feature, but don't

trunk/src/doc/reference.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ grammar as double-quoted strings. Other tokens have exact rules given.
187187

188188
The keywords are the following strings, organized by first letter:
189189

190+
<div id="keywords">
191+
| | | | |
190192
|----------|--------|--------|-------|
191193
| as | | | |
192194
|----------|--------|--------|-------|
@@ -216,6 +218,7 @@ The keywords are the following strings, organized by first letter:
216218
|----------|--------|--------|-------|
217219
| while | | | |
218220
|----------|--------|--------|-------|
221+
</div>
219222

220223
Each of these keywords has special meaning in its grammar, and all of them are
221224
excluded from the `ident` rule.

trunk/src/doc/rust.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,3 +392,5 @@ pre.rust { position: relative; }
392392
background-color: #fff !important;
393393
}
394394
}
395+
396+
#keywords table td { border: none; }

trunk/src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ syn match rustMacroRepeatCount ".\?[*+]" contained
5454
syn match rustMacroVariable "$\w\+"
5555

5656
" Reserved (but not yet used) keywords {{{2
57-
syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield
57+
syn keyword rustReservedKeyword alignof be do offsetof priv pure sizeof typeof unsized yield abstract final override
5858

5959
" Built-in types {{{2
6060
syn keyword rustType int uint float char bool u8 u16 u32 u64 f32

trunk/src/libcollections/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ use vec::Vec;
7575
pub use core::str::{from_utf8, CharEq, Chars, CharOffsets};
7676
pub use core::str::{Bytes, CharSplits};
7777
pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits};
78-
pub use core::str::{eq_slice, is_utf8, is_utf16, Utf16Items};
78+
pub use core::str::{Utf16CodeUnits, eq_slice, is_utf8, is_utf16, Utf16Items};
7979
pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items};
8080
pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange};
8181
pub use core::str::{Str, StrSlice};

trunk/src/libcollections/string.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use slice::CloneableVector;
2828
use str;
2929
use str::{CharRange, StrAllocating, MaybeOwned, Owned};
3030
use str::Slice as MaybeOwnedSlice; // So many `Slice`s...
31-
use vec::Vec;
31+
use vec::{DerefVec, Vec, as_vec};
3232

3333
/// A growable string stored as a UTF-8 encoded buffer.
3434
#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord)]
@@ -973,6 +973,24 @@ impl ops::Slice<uint, str> for String {
973973
}
974974
}
975975

976+
/// Wrapper type providing a `&String` reference via `Deref`.
977+
#[experimental]
978+
pub struct DerefString<'a> {
979+
x: DerefVec<'a, u8>
980+
}
981+
982+
impl<'a> Deref<String> for DerefString<'a> {
983+
fn deref<'b>(&'b self) -> &'b String {
984+
unsafe { mem::transmute(&*self.x) }
985+
}
986+
}
987+
988+
/// Convert a string slice to a wrapper type providing a `&String` reference.
989+
#[experimental]
990+
pub fn as_string<'a>(x: &'a str) -> DerefString<'a> {
991+
DerefString { x: as_vec(x.as_bytes()) }
992+
}
993+
976994
/// Unsafe operations
977995
#[unstable = "waiting on raw module conventions"]
978996
pub mod raw {
@@ -1039,9 +1057,15 @@ mod tests {
10391057
use {Mutable, MutableSeq};
10401058
use str;
10411059
use str::{Str, StrSlice, Owned};
1042-
use super::String;
1060+
use super::{as_string, String};
10431061
use vec::Vec;
10441062

1063+
#[test]
1064+
fn test_as_string() {
1065+
let x = "foo";
1066+
assert_eq!(x, as_string(x).as_slice());
1067+
}
1068+
10451069
#[test]
10461070
fn test_from_str() {
10471071
let owned: Option<::std::string::String> = from_str("string");

trunk/src/libcollections/vec.rs

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use alloc::heap::{EMPTY, allocate, reallocate, deallocate};
1818
use core::cmp::max;
1919
use core::default::Default;
2020
use core::fmt;
21-
use core::kinds::marker::InvariantType;
21+
use core::kinds::marker::{ContravariantLifetime, InvariantType};
2222
use core::mem;
2323
use core::num;
2424
use core::ops;
@@ -1875,6 +1875,39 @@ pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (Vec<T>, Vec<U>) {
18751875
(ts, us)
18761876
}
18771877

1878+
/// Wrapper type providing a `&Vec<T>` reference via `Deref`.
1879+
#[experimental]
1880+
pub struct DerefVec<'a, T> {
1881+
x: Vec<T>,
1882+
l: ContravariantLifetime<'a>
1883+
}
1884+
1885+
impl<'a, T> Deref<Vec<T>> for DerefVec<'a, T> {
1886+
fn deref<'b>(&'b self) -> &'b Vec<T> {
1887+
&self.x
1888+
}
1889+
}
1890+
1891+
// Prevent the inner `Vec<T>` from attempting to deallocate memory.
1892+
#[unsafe_destructor]
1893+
impl<'a, T> Drop for DerefVec<'a, T> {
1894+
fn drop(&mut self) {
1895+
self.x.len = 0;
1896+
self.x.cap = 0;
1897+
}
1898+
}
1899+
1900+
/// Convert a slice to a wrapper type providing a `&Vec<T>` reference.
1901+
#[experimental]
1902+
pub fn as_vec<'a, T>(x: &'a [T]) -> DerefVec<'a, T> {
1903+
unsafe {
1904+
DerefVec {
1905+
x: Vec::from_raw_parts(x.len(), x.len(), x.as_ptr() as *mut T),
1906+
l: ContravariantLifetime::<'a>
1907+
}
1908+
}
1909+
}
1910+
18781911
/// Unsafe vector operations.
18791912
#[unstable]
18801913
pub mod raw {
@@ -2169,10 +2202,38 @@ mod tests {
21692202
use std::prelude::*;
21702203
use std::mem::size_of;
21712204
use test::Bencher;
2172-
use super::{unzip, raw, Vec};
2205+
use super::{as_vec, unzip, raw, Vec};
21732206

21742207
use MutableSeq;
21752208

2209+
struct DropCounter<'a> {
2210+
count: &'a mut int
2211+
}
2212+
2213+
#[unsafe_destructor]
2214+
impl<'a> Drop for DropCounter<'a> {
2215+
fn drop(&mut self) {
2216+
*self.count += 1;
2217+
}
2218+
}
2219+
2220+
#[test]
2221+
fn test_as_vec() {
2222+
let xs = [1u8, 2u8, 3u8];
2223+
assert_eq!(as_vec(xs).as_slice(), xs.as_slice());
2224+
}
2225+
2226+
#[test]
2227+
fn test_as_vec_dtor() {
2228+
let (mut count_x, mut count_y) = (0, 0);
2229+
{
2230+
let xs = &[DropCounter { count: &mut count_x }, DropCounter { count: &mut count_y }];
2231+
assert_eq!(as_vec(xs).len(), 2);
2232+
}
2233+
assert_eq!(count_x, 1);
2234+
assert_eq!(count_y, 1);
2235+
}
2236+
21762237
#[test]
21772238
fn test_small_vec_struct() {
21782239
assert!(size_of::<Vec<u8>>() == size_of::<uint>() * 3);
@@ -2185,17 +2246,6 @@ mod tests {
21852246
y: Vec<T>
21862247
}
21872248

2188-
struct DropCounter<'a> {
2189-
count: &'a mut int
2190-
}
2191-
2192-
#[unsafe_destructor]
2193-
impl<'a> Drop for DropCounter<'a> {
2194-
fn drop(&mut self) {
2195-
*self.count += 1;
2196-
}
2197-
}
2198-
21992249
let (mut count_x, mut count_y) = (0, 0);
22002250
{
22012251
let mut tv = TwoVec {

trunk/src/libcore/slice.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,3 +1945,61 @@ impl<'a, T: PartialOrd> PartialOrd for &'a [T] {
19451945
order::gt(self.iter(), other.iter())
19461946
}
19471947
}
1948+
1949+
/// Extension methods for immutable slices containing integers.
1950+
#[experimental]
1951+
pub trait ImmutableIntSlice<'a, U, S> {
1952+
/// Converts the slice to an immutable slice of unsigned integers with the same width.
1953+
fn as_unsigned(self) -> &'a [U];
1954+
/// Converts the slice to an immutable slice of signed integers with the same width.
1955+
fn as_signed(self) -> &'a [S];
1956+
}
1957+
1958+
/// Extension methods for mutable slices containing integers.
1959+
#[experimental]
1960+
pub trait MutableIntSlice<'a, U, S>: ImmutableIntSlice<'a, U, S> {
1961+
/// Converts the slice to a mutable slice of unsigned integers with the same width.
1962+
fn as_unsigned_mut(self) -> &'a mut [U];
1963+
/// Converts the slice to a mutable slice of signed integers with the same width.
1964+
fn as_signed_mut(self) -> &'a mut [S];
1965+
}
1966+
1967+
macro_rules! impl_immut_int_slice {
1968+
($u:ty, $s:ty, $t:ty) => {
1969+
#[experimental]
1970+
impl<'a> ImmutableIntSlice<'a, $u, $s> for $t {
1971+
#[inline]
1972+
fn as_unsigned(self) -> &'a [$u] { unsafe { transmute(self) } }
1973+
#[inline]
1974+
fn as_signed(self) -> &'a [$s] { unsafe { transmute(self) } }
1975+
}
1976+
}
1977+
}
1978+
macro_rules! impl_mut_int_slice {
1979+
($u:ty, $s:ty, $t:ty) => {
1980+
#[experimental]
1981+
impl<'a> MutableIntSlice<'a, $u, $s> for $t {
1982+
#[inline]
1983+
fn as_unsigned_mut(self) -> &'a mut [$u] { unsafe { transmute(self) } }
1984+
#[inline]
1985+
fn as_signed_mut(self) -> &'a mut [$s] { unsafe { transmute(self) } }
1986+
}
1987+
}
1988+
}
1989+
1990+
macro_rules! impl_int_slice {
1991+
($u:ty, $s:ty) => {
1992+
impl_immut_int_slice!($u, $s, &'a [$u])
1993+
impl_immut_int_slice!($u, $s, &'a [$s])
1994+
impl_immut_int_slice!($u, $s, &'a mut [$u])
1995+
impl_immut_int_slice!($u, $s, &'a mut [$s])
1996+
impl_mut_int_slice!($u, $s, &'a mut [$u])
1997+
impl_mut_int_slice!($u, $s, &'a mut [$s])
1998+
}
1999+
}
2000+
2001+
impl_int_slice!(u8, i8)
2002+
impl_int_slice!(u16, i16)
2003+
impl_int_slice!(u32, i32)
2004+
impl_int_slice!(u64, i64)
2005+
impl_int_slice!(uint, int)

0 commit comments

Comments
 (0)