Skip to content

Commit

Permalink
auto merge of rust-lang#18596 : alexcrichton/rust/rollup, r=alexcrichton
Browse files Browse the repository at this point in the history
Let's see if we can clear out the queue entirely today!
  • Loading branch information
bors committed Nov 4, 2014
2 parents ec28b4a + f2aa8c4 commit 82fb413
Show file tree
Hide file tree
Showing 67 changed files with 1,242 additions and 1,331 deletions.
41 changes: 21 additions & 20 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -4467,18 +4467,19 @@ see why consumers matter.

## Iterators

As we've said before, an iterator is something that we can call the `.next()`
method on repeatedly, and it gives us a sequence of things. Because you need
to call the method, this means that iterators are **lazy**. This code, for
example, does not actually generate the numbers `1-100`, and just creates a
value that represents the sequence:
As we've said before, an iterator is something that we can call the
`.next()` method on repeatedly, and it gives us a sequence of things.
Because you need to call the method, this means that iterators
are **lazy** and don't need to generate all of the values upfront.
This code, for example, does not actually generate the numbers
`1-100`, and just creates a value that represents the sequence:

```{rust}
let nums = range(1i, 100i);
```

Since we didn't do anything with the range, it didn't generate the sequence.
Once we add the consumer:
Let's add the consumer:

```{rust}
let nums = range(1i, 100i).collect::<Vec<int>>();
Expand Down Expand Up @@ -4507,8 +4508,8 @@ std::iter::count(1i, 5i);
```

This iterator counts up from one, adding five each time. It will give
you a new integer every time, forever. Well, technically, until the
maximum number that an `int` can represent. But since iterators are lazy,
you a new integer every time, forever (well, technically, until it reaches the
maximum number representable by an `int`). But since iterators are lazy,
that's okay! You probably don't want to use `collect()` on it, though...

That's enough about iterators. Iterator adapters are the last concept
Expand Down Expand Up @@ -5251,8 +5252,8 @@ to do something that it can't currently do? You may be able to write a macro
to extend Rust's capabilities.

You've already used one macro extensively: `println!`. When we invoke
a Rust macro, we need to use the exclamation mark (`!`). There's two reasons
that this is true: the first is that it makes it clear when you're using a
a Rust macro, we need to use the exclamation mark (`!`). There are two reasons
why this is so: the first is that it makes it clear when you're using a
macro. The second is that macros allow for flexible syntax, and so Rust must
be able to tell where a macro starts and ends. The `!(...)` helps with this.

Expand All @@ -5267,7 +5268,7 @@ println!("x is: {}", x);

The `println!` macro does a few things:

1. It parses the string to find any `{}`s
1. It parses the string to find any `{}`s.
2. It checks that the number of `{}`s matches the number of other arguments.
3. It generates a bunch of Rust code, taking this in mind.

Expand All @@ -5276,8 +5277,8 @@ Rust will generate code that takes all of the types into account. If
`println!` was a function, it could still do this type checking, but it
would happen at run time rather than compile time.

We can check this out using a special flag to `rustc`. This code, in a file
`print.rs`:
We can check this out using a special flag to `rustc`. Put this code in a file
called `print.rs`:

```{rust}
fn main() {
Expand All @@ -5286,7 +5287,7 @@ fn main() {
}
```

Can have its macros expanded like this: `rustc print.rs --pretty=expanded`, will
You can have the macros expanded like this: `rustc print.rs --pretty=expanded` – which will
give us this huge result:

```{rust,ignore}
Expand Down Expand Up @@ -5325,12 +5326,12 @@ invoke the `println_args` function with the generated arguments.
This is the code that Rust actually compiles. You can see all of the extra
information that's here. We get all of the type safety and options that it
provides, but at compile time, and without needing to type all of this out.
This is how macros are powerful. Without them, you would need to type all of
this by hand to get a type checked `println`.
This is how macros are powerful: without them you would need to type all of
this by hand to get a type-checked `println`.

For more on macros, please consult [the Macros Guide](guide-macros.html).
Macros are a very advanced and still slightly experimental feature, but don't
require a deep understanding to call, since they look just like functions. The
Macros are a very advanced and still slightly experimental feature, but they don't
require a deep understanding to be called, since they look just like functions. The
Guide can help you if you want to write your own.

# Unsafe
Expand All @@ -5347,8 +5348,8 @@ keyword, which indicates that the function may not behave properly.

Second, if you'd like to create some sort of shared-memory data structure, Rust
won't allow it, because memory must be owned by a single owner. However, if
you're planning on making access to that shared memory safe, such as with a
mutex, _you_ know that it's safe, but Rust can't know. Writing an `unsafe`
you're planning on making access to that shared memory safe such as with a
mutex _you_ know that it's safe, but Rust can't know. Writing an `unsafe`
block allows you to ask the compiler to trust you. In this case, the _internal_
implementation of the mutex is considered unsafe, but the _external_ interface
we present is safe. This allows it to be effectively used in normal Rust, while
Expand Down
8 changes: 4 additions & 4 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ mod math {
}
```

Modules and types share the same namespace. Declaring a named type that has
Modules and types share the same namespace. Declaring a named type with
the same name as a module in scope is forbidden: that is, a type definition,
trait, struct, enumeration, or type parameter can't shadow the name of a module
in scope, or vice versa.
Expand Down Expand Up @@ -870,8 +870,8 @@ view_item : extern_crate_decl | use_decl ;
```

A view item manages the namespace of a module. View items do not define new
items, but rather, simply change other items' visibility. There are several
kinds of view item:
items, but rather, simply change other items' visibility. There are two
kinds of view items:

* [`extern crate` declarations](#extern-crate-declarations)
* [`use` declarations](#use-declarations)
Expand All @@ -896,7 +896,7 @@ external crate when it was compiled. If no `crateid` is provided, a default
`name` attribute is assumed, equal to the `ident` given in the
`extern_crate_decl`.

Four examples of `extern crate` declarations:
Three examples of `extern crate` declarations:

```{.ignore}
extern crate pcre;
Expand Down
12 changes: 6 additions & 6 deletions src/etc/vim/autoload/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,14 @@ function! s:WithPath(func, ...)
call mkdir(tmpdir)

let save_cwd = getcwd()
silent exe 'lcd' tmpdir
silent exe 'lcd' fnameescape(tmpdir)

let path = 'unnamed.rs'

let save_mod = &mod
set nomod

silent exe 'keepalt write! ' . path
silent exe 'keepalt write! ' . fnameescape(path)
if pathisempty
silent keepalt 0file
endif
Expand All @@ -195,10 +195,10 @@ function! s:WithPath(func, ...)

call call(a:func, [path] + a:000)
finally
if exists("save_mod") | let &mod = save_mod | endif
if exists("save_write") | let &write = save_write | endif
if exists("save_cwd") | silent exe 'lcd' save_cwd | endif
if exists("tmpdir") | silent call s:RmDir(tmpdir) | endif
if exists("save_mod") | let &mod = save_mod | endif
if exists("save_write") | let &write = save_write | endif
if exists("save_cwd") | silent exe 'lcd' fnameescape(save_cwd) | endif
if exists("tmpdir") | silent call s:RmDir(tmpdir) | endif
endtry
endfunction

Expand Down
1 change: 0 additions & 1 deletion src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@
#![stable]

use clone::Clone;
use cmp::PartialEq;
use std::fmt::Show;
use slice;
Expand Down
1 change: 0 additions & 1 deletion src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use mem;
use char;
use char::Char;
use clone::Clone;
use cmp;
use cmp::{PartialEq, Eq};
use default::Default;
Expand Down
7 changes: 7 additions & 0 deletions src/librand/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ fn ziggurat<R:Rng>(
// creating a f64), so we might as well reuse some to save
// generating a whole extra random number. (Seems to be 15%
// faster.)
//
// This unfortunately misses out on the benefits of direct
// floating point generation if an RNG like dSMFT is
// used. (That is, such RNGs create floats directly, highly
// efficiently and overload next_f32/f64, so by not calling it
// this may be slower than it would be otherwise.)
// FIXME: investigate/optimise for the above.
let bits: u64 = rng.gen();
let i = (bits & 0xff) as uint;
let f = (bits >> 11) as f64 / SCALE;
Expand Down
40 changes: 40 additions & 0 deletions src/librand/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,46 @@ pub trait Rng {
(self.next_u32() as u64 << 32) | (self.next_u32() as u64)
}

/// Return the next random f32 selected from the half-open
/// interval `[0, 1)`.
///
/// By default this is implemented in terms of `next_u32`, but a
/// random number generator which can generate numbers satisfying
/// the requirements directly can overload this for performance.
/// It is required that the return value lies in `[0, 1)`.
///
/// See `Closed01` for the closed interval `[0,1]`, and
/// `Open01` for the open interval `(0,1)`.
fn next_f32(&mut self) -> f32 {
const MANTISSA_BITS: uint = 24;
const IGNORED_BITS: uint = 8;
const SCALE: f32 = (1u64 << MANTISSA_BITS) as f32;

// using any more than `MANTISSA_BITS` bits will
// cause (e.g.) 0xffff_ffff to correspond to 1
// exactly, so we need to drop some (8 for f32, 11
// for f64) to guarantee the open end.
(self.next_u32() >> IGNORED_BITS) as f32 / SCALE
}

/// Return the next random f64 selected from the half-open
/// interval `[0, 1)`.
///
/// By default this is implemented in terms of `next_u64`, but a
/// random number generator which can generate numbers satisfying
/// the requirements directly can overload this for performance.
/// It is required that the return value lies in `[0, 1)`.
///
/// See `Closed01` for the closed interval `[0,1]`, and
/// `Open01` for the open interval `(0,1)`.
fn next_f64(&mut self) -> f64 {
const MANTISSA_BITS: uint = 53;
const IGNORED_BITS: uint = 11;
const SCALE: f64 = (1u64 << MANTISSA_BITS) as f64;

(self.next_u64() >> IGNORED_BITS) as f64 / SCALE
}

/// Fill `dest` with random data.
///
/// This has a default implementation in terms of `next_u64` and
Expand Down
23 changes: 9 additions & 14 deletions src/librand/rand_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ impl Rand for u64 {
}

macro_rules! float_impls {
($mod_name:ident, $ty:ty, $mantissa_bits:expr, $method_name:ident, $ignored_bits:expr) => {
($mod_name:ident, $ty:ty, $mantissa_bits:expr, $method_name:ident) => {
mod $mod_name {
use {Rand, Rng, Open01, Closed01};

static SCALE: $ty = (1u64 << $mantissa_bits) as $ty;
const SCALE: $ty = (1u64 << $mantissa_bits) as $ty;

impl Rand for $ty {
/// Generate a floating point number in the half-open
Expand All @@ -110,11 +110,7 @@ macro_rules! float_impls {
/// and `Open01` for the open interval `(0,1)`.
#[inline]
fn rand<R: Rng>(rng: &mut R) -> $ty {
// using any more than `mantissa_bits` bits will
// cause (e.g.) 0xffff_ffff to correspond to 1
// exactly, so we need to drop some (8 for f32, 11
// for f64) to guarantee the open end.
(rng.$method_name() >> $ignored_bits) as $ty / SCALE
rng.$method_name()
}
}
impl Rand for Open01<$ty> {
Expand All @@ -124,23 +120,22 @@ macro_rules! float_impls {
// the precision of f64/f32 at 1.0), so that small
// numbers are larger than 0, but large numbers
// aren't pushed to/above 1.
Open01(((rng.$method_name() >> $ignored_bits) as $ty + 0.25) / SCALE)
Open01(rng.$method_name() + 0.25 / SCALE)
}
}
impl Rand for Closed01<$ty> {
#[inline]
fn rand<R: Rng>(rng: &mut R) -> Closed01<$ty> {
// divide by the maximum value of the numerator to
// get a non-zero probability of getting exactly
// 1.0.
Closed01((rng.$method_name() >> $ignored_bits) as $ty / (SCALE - 1.0))
// rescale so that 1.0 - epsilon becomes 1.0
// precisely.
Closed01(rng.$method_name() * SCALE / (SCALE - 1.0))
}
}
}
}
}
float_impls! { f64_rand_impls, f64, 53, next_u64, 11 }
float_impls! { f32_rand_impls, f32, 24, next_u32, 8 }
float_impls! { f64_rand_impls, f64, 53, next_f64 }
float_impls! { f32_rand_impls, f32, 24, next_f32 }

impl Rand for char {
#[inline]
Expand Down
20 changes: 10 additions & 10 deletions src/librustc/lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::cmp;
use std::collections::HashMap;
use std::collections::hash_map::{Occupied, Vacant};
use std::slice;
use std::{int, i8, i16, i32, i64, uint, u8, u16, u32, u64, f32, f64};
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
use syntax::abi;
use syntax::ast_map;
use syntax::ast_util::is_shift_binop;
Expand Down Expand Up @@ -180,19 +180,19 @@ impl LintPass for TypeLimits {

if is_shift_binop(binop) {
let opt_ty_bits = match ty::get(ty::expr_ty(cx.tcx, &**l)).sty {
ty::ty_int(t) => Some(int_ty_bits(t)),
ty::ty_uint(t) => Some(uint_ty_bits(t)),
ty::ty_int(t) => Some(int_ty_bits(t, cx.sess().targ_cfg.int_type)),
ty::ty_uint(t) => Some(uint_ty_bits(t, cx.sess().targ_cfg.uint_type)),
_ => None
};

if let Some(bits) = opt_ty_bits {
let exceeding = if let ast::ExprLit(ref lit) = r.node {
if let ast::LitInt(shift, _) = lit.node { shift > bits }
if let ast::LitInt(shift, _) = lit.node { shift >= bits }
else { false }
} else {
match eval_const_expr_partial(cx.tcx, &**r) {
Ok(const_int(shift)) => { shift as u64 > bits },
Ok(const_uint(shift)) => { shift > bits },
Ok(const_int(shift)) => { shift as u64 >= bits },
Ok(const_uint(shift)) => { shift >= bits },
_ => { false }
}
};
Expand Down Expand Up @@ -312,19 +312,19 @@ impl LintPass for TypeLimits {
}
}

fn int_ty_bits(int_ty: ast::IntTy) -> u64 {
fn int_ty_bits(int_ty: ast::IntTy, target_int_ty: ast::IntTy) -> u64 {
match int_ty {
ast::TyI => int::BITS as u64,
ast::TyI => int_ty_bits(target_int_ty, target_int_ty),
ast::TyI8 => i8::BITS as u64,
ast::TyI16 => i16::BITS as u64,
ast::TyI32 => i32::BITS as u64,
ast::TyI64 => i64::BITS as u64
}
}

fn uint_ty_bits(uint_ty: ast::UintTy) -> u64 {
fn uint_ty_bits(uint_ty: ast::UintTy, target_uint_ty: ast::UintTy) -> u64 {
match uint_ty {
ast::TyU => uint::BITS as u64,
ast::TyU => uint_ty_bits(target_uint_ty, target_uint_ty),
ast::TyU8 => u8::BITS as u64,
ast::TyU16 => u16::BITS as u64,
ast::TyU32 => u32::BITS as u64,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
}
}

fn visit_ids(&self, f: |&mut ast_util::IdVisitor<Context>|) {
fn visit_ids(&mut self, f: |&mut ast_util::IdVisitor<Context>|) {
let mut v = ast_util::IdVisitor {
operation: self,
pass_through_items: false,
Expand Down Expand Up @@ -749,7 +749,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {

// Output any lints that were previously added to the session.
impl<'a, 'tcx> IdVisitingOperation for Context<'a, 'tcx> {
fn visit_id(&self, id: ast::NodeId) {
fn visit_id(&mut self, id: ast::NodeId) {
match self.tcx.sess.lints.borrow_mut().pop(&id) {
None => {}
Some(lints) => {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl astencode_tag {
pub fn from_uint(value : uint) -> Option<astencode_tag> {
let is_a_tag = first_astencode_tag <= value && value <= last_astencode_tag;
if !is_a_tag { None } else {
Some(unsafe { mem::transmute(value) })
Some(unsafe { mem::transmute::<uint, astencode_tag>(value) })
}
}
}
Expand Down Expand Up @@ -247,4 +247,3 @@ pub const tag_type_param_def: uint = 0xa5;

pub const tag_item_generics: uint = 0xa6;
pub const tag_method_ty_generics: uint = 0xa7;

Loading

0 comments on commit 82fb413

Please sign in to comment.