Skip to content

Commit 103aee5

Browse files
committed
---
yaml --- r: 152993 b: refs/heads/try2 c: 6ab7b66 h: refs/heads/master i: 152991: 9a7d82a v: v3
1 parent 360bb47 commit 103aee5

File tree

35 files changed

+1017
-706
lines changed

35 files changed

+1017
-706
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: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: fa5bc6f1bd703cb081b48d032713bf78499bf155
8+
refs/heads/try2: 6ab7b6652ec916e37a2110bb2f579c7dc38b4560
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/doc/guide.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ Save the file, and then type this into your terminal window:
160160

161161
```{bash}
162162
$ rustc hello_world.rs
163-
$ ./hello_world # just 'hello_world' on Windows
163+
$ ./hello_world # or hello_world.exe on Windows
164164
Hello, world
165165
```
166166

@@ -243,7 +243,7 @@ There are now two files: our source code, with the `.rs` extension, and the
243243
executable (`hello_world.exe` on Windows, `hello_world` everywhere else)
244244

245245
```{bash}
246-
$ ./hello_world # or ./hello_world.exe on Windows
246+
$ ./hello_world # or hello_world.exe on Windows
247247
```
248248

249249
This prints out our `Hello, world!` text to our terminal.

branches/try2/src/libcollections/str.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ pub fn from_utf8_owned(vv: Vec<u8>) -> Result<String, Vec<u8>> {
106106
/// # Failure
107107
///
108108
/// Fails if invalid UTF-8
109+
///
110+
/// # Example
111+
///
112+
/// ```rust
113+
/// use std::str;
114+
/// let string = str::from_byte(66u8);
115+
/// assert_eq!(string.as_slice(), "B");
116+
/// ```
109117
pub fn from_byte(b: u8) -> String {
110118
assert!(b < 128u8);
111119
String::from_char(1, b as char)
@@ -803,15 +811,9 @@ pub trait StrAllocating: Str {
803811
}
804812

805813
/// Converts to a vector of `u16` encoded as UTF-16.
814+
#[deprecated = "use `utf16_units` instead"]
806815
fn to_utf16(&self) -> Vec<u16> {
807-
let me = self.as_slice();
808-
let mut u = Vec::new();
809-
for ch in me.chars() {
810-
let mut buf = [0u16, ..2];
811-
let n = ch.encode_utf16(buf /* as mut slice! */);
812-
u.push_all(buf.slice_to(n));
813-
}
814-
u
816+
self.as_slice().utf16_units().collect::<Vec<u16>>()
815817
}
816818

817819
/// Given a string, make a new string with repeated copies of it.
@@ -1619,14 +1621,17 @@ mod tests {
16191621

16201622
for p in pairs.iter() {
16211623
let (s, u) = (*p).clone();
1624+
let s_as_utf16 = s.as_slice().utf16_units().collect::<Vec<u16>>();
1625+
let u_as_string = from_utf16(u.as_slice()).unwrap();
1626+
16221627
assert!(is_utf16(u.as_slice()));
1623-
assert_eq!(s.to_utf16(), u);
1628+
assert_eq!(s_as_utf16, u);
16241629

1625-
assert_eq!(from_utf16(u.as_slice()).unwrap(), s);
1630+
assert_eq!(u_as_string, s);
16261631
assert_eq!(from_utf16_lossy(u.as_slice()), s);
16271632

1628-
assert_eq!(from_utf16(s.to_utf16().as_slice()).unwrap(), s);
1629-
assert_eq!(from_utf16(u.as_slice()).unwrap().to_utf16(), u);
1633+
assert_eq!(from_utf16(s_as_utf16.as_slice()).unwrap(), s);
1634+
assert_eq!(u_as_string.as_slice().utf16_units().collect::<Vec<u16>>(), u);
16301635
}
16311636
}
16321637

branches/try2/src/libcore/fmt/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ impl<'a, T: Show> Show for &'a T {
518518
impl<'a, T: Show> Show for &'a mut T {
519519
fn fmt(&self, f: &mut Formatter) -> Result { secret_show(&**self, f) }
520520
}
521+
impl<'a> Show for &'a Show {
522+
fn fmt(&self, f: &mut Formatter) -> Result { (*self).fmt(f) }
523+
}
521524

522525
impl Bool for bool {
523526
fn fmt(&self, f: &mut Formatter) -> Result {

branches/try2/src/libcore/str.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use mem;
1818
use char;
19+
use char::Char;
1920
use clone::Clone;
2021
use cmp;
2122
use cmp::{PartialEq, Eq};
@@ -24,7 +25,7 @@ use default::Default;
2425
use iter::{Filter, Map, Iterator};
2526
use iter::{DoubleEndedIterator, ExactSize};
2627
use iter::range;
27-
use num::Saturating;
28+
use num::{CheckedMul, Saturating};
2829
use option::{None, Option, Some};
2930
use raw::Repr;
3031
use slice::ImmutableVector;
@@ -557,6 +558,41 @@ impl<'a> Iterator<&'a str> for StrSplits<'a> {
557558
}
558559
}
559560

561+
/// External iterator for a string's UTF16 codeunits.
562+
/// Use with the `std::iter` module.
563+
#[deriving(Clone)]
564+
pub struct Utf16CodeUnits<'a> {
565+
chars: Chars<'a>,
566+
extra: u16
567+
}
568+
569+
impl<'a> Iterator<u16> for Utf16CodeUnits<'a> {
570+
#[inline]
571+
fn next(&mut self) -> Option<u16> {
572+
if self.extra != 0 {
573+
let tmp = self.extra;
574+
self.extra = 0;
575+
return Some(tmp);
576+
}
577+
578+
let mut buf = [0u16, ..2];
579+
self.chars.next().map(|ch| {
580+
let n = ch.encode_utf16(buf /* as mut slice! */);
581+
if n == 2 { self.extra = buf[1]; }
582+
buf[0]
583+
})
584+
}
585+
586+
#[inline]
587+
fn size_hint(&self) -> (uint, Option<uint>) {
588+
let (low, high) = self.chars.size_hint();
589+
// every char gets either one u16 or two u16,
590+
// so this iterator is between 1 or 2 times as
591+
// long as the underlying iterator.
592+
(low, high.and_then(|n| n.checked_mul(&2)))
593+
}
594+
}
595+
560596
/*
561597
Section: Comparing strings
562598
*/
@@ -1609,6 +1645,9 @@ pub trait StrSlice<'a> {
16091645
/// and that it is not reallocated (e.g. by pushing to the
16101646
/// string).
16111647
fn as_ptr(&self) -> *const u8;
1648+
1649+
/// Return an iterator of `u16` over the string encoded as UTF-16.
1650+
fn utf16_units(&self) -> Utf16CodeUnits<'a>;
16121651
}
16131652

16141653
impl<'a> StrSlice<'a> for &'a str {
@@ -1957,6 +1996,11 @@ impl<'a> StrSlice<'a> for &'a str {
19571996
fn as_ptr(&self) -> *const u8 {
19581997
self.repr().data
19591998
}
1999+
2000+
#[inline]
2001+
fn utf16_units(&self) -> Utf16CodeUnits<'a> {
2002+
Utf16CodeUnits{ chars: self.chars(), extra: 0}
2003+
}
19602004
}
19612005

19622006
impl<'a> Default for &'a str {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ extern "system" {
7070

7171
pub mod compat {
7272
use std::intrinsics::{atomic_store_relaxed, transmute};
73+
use std::iter::Iterator;
7374
use libc::types::os::arch::extra::{LPCWSTR, HMODULE, LPCSTR, LPVOID};
7475

7576
extern "system" {
@@ -82,7 +83,8 @@ pub mod compat {
8283
// layer (after it's loaded) shouldn't be any slower than a regular DLL
8384
// call.
8485
unsafe fn store_func(ptr: *mut uint, module: &str, symbol: &str, fallback: uint) {
85-
let module = module.to_utf16().append_one(0);
86+
let module: Vec<u16> = module.utf16_units().collect();
87+
let module = module.append_one(0);
8688
symbol.with_c_str(|symbol| {
8789
let handle = GetModuleHandleW(module.as_ptr());
8890
let func: uint = transmute(GetProcAddress(handle, symbol));

branches/try2/src/libnative/io/file_win32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl Drop for Inner {
255255

256256
pub fn to_utf16(s: &CString) -> IoResult<Vec<u16>> {
257257
match s.as_str() {
258-
Some(s) => Ok(s.to_utf16().append_one(0)),
258+
Some(s) => Ok(s.utf16_units().collect::<Vec<u16>>().append_one(0)),
259259
None => Err(IoError {
260260
code: libc::ERROR_INVALID_NAME as uint,
261261
extra: 0,

branches/try2/src/libnative/io/process.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ fn spawn_process_os(cfg: ProcessConfig,
294294
use libc::funcs::extra::msvcrt::get_osfhandle;
295295

296296
use std::mem;
297+
use std::iter::Iterator;
298+
use std::str::StrSlice;
297299

298300
if cfg.gid.is_some() || cfg.uid.is_some() {
299301
return Err(IoError {
@@ -328,7 +330,8 @@ fn spawn_process_os(cfg: ProcessConfig,
328330
lpSecurityDescriptor: ptr::mut_null(),
329331
bInheritHandle: 1,
330332
};
331-
let filename = "NUL".to_utf16().append_one(0);
333+
let filename: Vec<u16> = "NUL".utf16_units().collect();
334+
let filename = filename.append_one(0);
332335
*slot = libc::CreateFileW(filename.as_ptr(),
333336
access,
334337
libc::FILE_SHARE_READ |
@@ -371,7 +374,8 @@ fn spawn_process_os(cfg: ProcessConfig,
371374

372375
with_envp(cfg.env, |envp| {
373376
with_dirp(cfg.cwd, |dirp| {
374-
let mut cmd_str = cmd_str.to_utf16().append_one(0);
377+
let mut cmd_str: Vec<u16> = cmd_str.as_slice().utf16_units().collect();
378+
cmd_str = cmd_str.append_one(0);
375379
let created = CreateProcessW(ptr::null(),
376380
cmd_str.as_mut_ptr(),
377381
ptr::mut_null(),
@@ -770,7 +774,7 @@ fn with_envp<T>(env: Option<&[(CString, CString)]>, cb: |*mut c_void| -> T) -> T
770774
let kv = format!("{}={}",
771775
pair.ref0().as_str().unwrap(),
772776
pair.ref1().as_str().unwrap());
773-
blk.push_all(kv.to_utf16().as_slice());
777+
blk.extend(kv.as_slice().utf16_units());
774778
blk.push(0);
775779
}
776780

@@ -788,7 +792,9 @@ fn with_dirp<T>(d: Option<&CString>, cb: |*const u16| -> T) -> T {
788792
Some(dir) => {
789793
let dir_str = dir.as_str()
790794
.expect("expected workingdirectory to be utf-8 encoded");
791-
let dir_str = dir_str.to_utf16().append_one(0);
795+
let dir_str: Vec<u16> = dir_str.utf16_units().collect();
796+
let dir_str = dir_str.append_one(0);
797+
792798
cb(dir_str.as_ptr())
793799
},
794800
None => cb(ptr::null())

branches/try2/src/librustc/lint/builtin.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use middle::def::*;
3030
use middle::trans::adt; // for `adt::is_ffi_safe`
3131
use middle::typeck::astconv::ast_ty_to_ty;
3232
use middle::typeck::infer;
33-
use middle::{typeck, ty, def, pat_util};
33+
use middle::{typeck, ty, def, pat_util, stability};
3434
use util::ppaux::{ty_to_str};
3535
use util::nodemap::NodeSet;
3636
use lint::{Context, LintPass, LintArray};
@@ -1426,11 +1426,7 @@ impl LintPass for Stability {
14261426
Some(method) => {
14271427
match method.origin {
14281428
typeck::MethodStatic(def_id) => {
1429-
// If this implements a trait method, get def_id
1430-
// of the method inside trait definition.
1431-
// Otherwise, use the current def_id (which refers
1432-
// to the method inside impl).
1433-
ty::trait_method_of_method(cx.tcx, def_id).unwrap_or(def_id)
1429+
def_id
14341430
}
14351431
typeck::MethodParam(typeck::MethodParam {
14361432
trait_id: trait_id,
@@ -1454,8 +1450,7 @@ impl LintPass for Stability {
14541450
// check anything for crate-local usage.
14551451
if ast_util::is_local(id) { return }
14561452

1457-
let stability = cx.tcx.stability.borrow_mut().lookup(&cx.tcx.sess.cstore, id);
1458-
1453+
let stability = stability::lookup(cx.tcx, id);
14591454
let (lint, label) = match stability {
14601455
// no stability attributes == Unstable
14611456
None => (UNSTABLE, "unmarked"),

branches/try2/src/librustc/metadata/encoder.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use middle::ty::{node_id_to_type, lookup_item_type};
2424
use middle::astencode;
2525
use middle::ty;
2626
use middle::typeck;
27+
use middle::stability;
2728
use middle;
2829
use util::nodemap::{NodeMap, NodeSet};
2930

@@ -328,7 +329,7 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
328329
encode_visibility(ebml_w, variant.node.vis);
329330
encode_attributes(ebml_w, variant.node.attrs.as_slice());
330331

331-
let stab = ecx.tcx.stability.borrow().lookup_local(variant.node.id);
332+
let stab = stability::lookup(ecx.tcx, ast_util::local_def(variant.node.id));
332333
encode_stability(ebml_w, stab);
333334

334335
match variant.node.kind {
@@ -592,7 +593,9 @@ fn encode_info_for_mod(ecx: &EncodeContext,
592593

593594
encode_path(ebml_w, path.clone());
594595
encode_visibility(ebml_w, vis);
595-
encode_stability(ebml_w, ecx.tcx.stability.borrow().lookup_local(id));
596+
597+
let stab = stability::lookup(ecx.tcx, ast_util::local_def(id));
598+
encode_stability(ebml_w, stab);
596599

597600
// Encode the reexports of this module, if this module is public.
598601
if vis == Public {
@@ -722,7 +725,8 @@ fn encode_info_for_struct_ctor(ecx: &EncodeContext,
722725
encode_symbol(ecx, ebml_w, ctor_id);
723726
}
724727

725-
encode_stability(ebml_w, ecx.tcx.stability.borrow().lookup_local(ctor_id));
728+
let stab = stability::lookup(ecx.tcx, ast_util::local_def(ctor_id));
729+
encode_stability(ebml_w, stab);
726730

727731
// indicate that this is a tuple struct ctor, because downstream users will normally want
728732
// the tuple struct definition, but without this there is no way for them to tell that
@@ -768,7 +772,7 @@ fn encode_info_for_method(ecx: &EncodeContext,
768772
encode_method_ty_fields(ecx, ebml_w, m);
769773
encode_parent_item(ebml_w, local_def(parent_id));
770774

771-
let stab = ecx.tcx.stability.borrow().lookup_local(m.def_id.node);
775+
let stab = stability::lookup(ecx.tcx, m.def_id);
772776
encode_stability(ebml_w, stab);
773777

774778
// The type for methods gets encoded twice, which is unfortunate.
@@ -915,10 +919,10 @@ fn encode_info_for_item(ecx: &EncodeContext,
915919
}
916920

917921
debug!("encoding info for item at {}",
918-
ecx.tcx.sess.codemap().span_to_str(item.span));
922+
tcx.sess.codemap().span_to_str(item.span));
919923

920924
let def_id = local_def(item.id);
921-
let stab = tcx.stability.borrow().lookup_local(item.id);
925+
let stab = stability::lookup(tcx, ast_util::local_def(item.id));
922926

923927
match item.node {
924928
ItemStatic(_, m, _) => {
@@ -1206,7 +1210,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
12061210
encode_method_ty_fields(ecx, ebml_w, &*method_ty);
12071211
encode_parent_item(ebml_w, def_id);
12081212

1209-
let stab = tcx.stability.borrow().lookup_local(method_def_id.node);
1213+
let stab = stability::lookup(tcx, method_def_id);
12101214
encode_stability(ebml_w, stab);
12111215

12121216
let elem = ast_map::PathName(method_ty.ident.name);

branches/try2/src/librustc/middle/stability.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ use syntax::ast::{Generics, StructDef, Ident};
2020
use syntax::ast_util::is_local;
2121
use syntax::attr::Stability;
2222
use syntax::visit::{FnKind, FkMethod, Visitor};
23-
use metadata::{cstore, csearch};
23+
use middle::ty;
24+
use metadata::csearch;
2425

2526
/// A stability index, giving the stability level for items and methods.
2627
pub struct Index {
@@ -105,21 +106,24 @@ impl Index {
105106
attr::find_stability(krate.attrs.as_slice()));
106107
annotator.index
107108
}
109+
}
108110

109-
/// Lookup the stability for a node, loading external crate
110-
/// metadata as necessary.
111-
pub fn lookup(&mut self, cstore: &cstore::CStore, id: DefId) -> Option<Stability> {
112-
if is_local(id) {
113-
self.lookup_local(id.node)
114-
} else {
115-
let stab = csearch::get_stability(cstore, id);
116-
self.extern_cache.insert(id, stab.clone());
111+
/// Lookup the stability for a node, loading external crate
112+
/// metadata as necessary.
113+
pub fn lookup(tcx: &ty::ctxt, id: DefId) -> Option<Stability> {
114+
// is this definition the implementation of a trait method?
115+
match ty::trait_method_of_method(tcx, id) {
116+
Some(trait_method_id) if trait_method_id != id => {
117+
lookup(tcx, trait_method_id)
118+
}
119+
_ if is_local(id) => {
120+
tcx.stability.borrow().local.find_copy(&id.node)
121+
}
122+
_ => {
123+
let stab = csearch::get_stability(&tcx.sess.cstore, id);
124+
let mut index = tcx.stability.borrow_mut();
125+
(*index).extern_cache.insert(id, stab.clone());
117126
stab
118127
}
119128
}
120-
121-
/// Lookup the stability for a local node without loading any external crates
122-
pub fn lookup_local(&self, id: NodeId) -> Option<Stability> {
123-
self.local.find_copy(&id)
124-
}
125129
}

0 commit comments

Comments
 (0)