Skip to content

Commit 9e801f4

Browse files
committed
---
yaml --- r: 94151 b: refs/heads/try c: 29ca435 h: refs/heads/master i: 94149: 73bd734 94147: f9d0310 94143: 671cfa1 v: v3
1 parent 48b8819 commit 9e801f4

File tree

8 files changed

+235
-35
lines changed

8 files changed

+235
-35
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
5-
refs/heads/try: fce4a174b9ffff71a66feecd9f4960f17fc9c331
5+
refs/heads/try: 29ca4350c8d64facb39311660e8ee919766f481a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/librustc/back/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ pub fn build_link_meta(sess: Session,
492492
}
493493

494494
fn len_and_str_lit(l: ast::lit) -> ~str {
495-
len_and_str(pprust::lit_to_str(@l))
495+
len_and_str(pprust::lit_to_str(&l))
496496
}
497497

498498
let cmh_items = attr::sort_meta_items(cmh_items);

branches/try/src/librustc/front/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn mk_test_module(cx: &TestCtxt) -> @ast::item {
330330
};
331331

332332
debug!("Synthetic test module:\n{}\n",
333-
pprust::item_to_str(@item.clone(), cx.sess.intr()));
333+
pprust::item_to_str(&item, cx.sess.intr()));
334334

335335
return @item;
336336
}

branches/try/src/librustc/middle/lint.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,27 +1028,47 @@ fn check_unused_mut_pat(cx: &Context, p: &ast::Pat) {
10281028
}
10291029
}
10301030

1031+
enum Allocation {
1032+
VectorAllocation,
1033+
BoxAllocation
1034+
}
1035+
10311036
fn check_unnecessary_allocation(cx: &Context, e: &ast::Expr) {
1032-
// Warn if string and vector literals with sigils are immediately borrowed.
1033-
// Those can have the sigil removed.
1034-
match e.node {
1037+
// Warn if string and vector literals with sigils, or boxing expressions,
1038+
// are immediately borrowed.
1039+
let allocation = match e.node {
10351040
ast::ExprVstore(e2, ast::ExprVstoreUniq) |
10361041
ast::ExprVstore(e2, ast::ExprVstoreBox) => {
10371042
match e2.node {
10381043
ast::ExprLit(@codemap::Spanned{node: ast::lit_str(..), ..}) |
1039-
ast::ExprVec(..) => {}
1044+
ast::ExprVec(..) => VectorAllocation,
10401045
_ => return
10411046
}
10421047
}
1048+
ast::ExprUnary(_, ast::UnUniq, _) |
1049+
ast::ExprUnary(_, ast::UnBox(..), _) => BoxAllocation,
10431050

10441051
_ => return
1045-
}
1052+
};
1053+
1054+
let report = |msg| {
1055+
cx.span_lint(unnecessary_allocation, e.span, msg);
1056+
};
10461057

10471058
match cx.tcx.adjustments.find_copy(&e.id) {
1048-
Some(@ty::AutoDerefRef(ty::AutoDerefRef {
1049-
autoref: Some(ty::AutoBorrowVec(..)), .. })) => {
1050-
cx.span_lint(unnecessary_allocation, e.span,
1051-
"unnecessary allocation, the sigil can be removed");
1059+
Some(@ty::AutoDerefRef(ty::AutoDerefRef { autoref, .. })) => {
1060+
match (allocation, autoref) {
1061+
(VectorAllocation, Some(ty::AutoBorrowVec(..))) => {
1062+
report("unnecessary allocation, the sigil can be removed");
1063+
}
1064+
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutImmutable))) => {
1065+
report("unnecessary allocation, use & instead");
1066+
}
1067+
(BoxAllocation, Some(ty::AutoPtr(_, ast::MutMutable))) => {
1068+
report("unnecessary allocation, use &mut instead");
1069+
}
1070+
_ => ()
1071+
}
10521072
}
10531073

10541074
_ => ()

branches/try/src/libstd/io/comm_adapters.rs

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

11-
use option::Option;
12-
use comm::{GenericPort, GenericChan};
11+
use prelude::*;
12+
13+
use comm::{GenericPort, GenericChan, GenericSmartChan};
14+
use cmp;
15+
use io;
16+
use option::{None, Option, Some};
1317
use super::{Reader, Writer};
18+
use vec::{bytes, CopyableVector, MutableVector, ImmutableVector};
1419

15-
pub struct PortReader<P>;
20+
/// Allows reading from a port.
21+
///
22+
/// # Example
23+
///
24+
/// ```
25+
/// let reader = PortReader::new(port);
26+
///
27+
/// let mut buf = ~[0u8, ..100];
28+
/// match reader.read(buf) {
29+
/// Some(nread) => println!("Read {} bytes", nread),
30+
/// None => println!("At the end of the stream!")
31+
/// }
32+
/// ```
33+
pub struct PortReader<P> {
34+
priv buf: Option<~[u8]>, // A buffer of bytes received but not consumed.
35+
priv pos: uint, // How many of the buffered bytes have already be consumed.
36+
priv port: P, // The port to pull data from.
37+
priv closed: bool, // Whether the pipe this port connects to has been closed.
38+
}
1639

1740
impl<P: GenericPort<~[u8]>> PortReader<P> {
18-
pub fn new(_port: P) -> PortReader<P> { fail!() }
41+
pub fn new(port: P) -> PortReader<P> {
42+
PortReader {
43+
buf: None,
44+
pos: 0,
45+
port: port,
46+
closed: false,
47+
}
48+
}
1949
}
2050

2151
impl<P: GenericPort<~[u8]>> Reader for PortReader<P> {
22-
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
52+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
53+
let mut num_read = 0;
54+
loop {
55+
match self.buf {
56+
Some(ref prev) => {
57+
let dst = buf.mut_slice_from(num_read);
58+
let src = prev.slice_from(self.pos);
59+
let count = cmp::min(dst.len(), src.len());
60+
bytes::copy_memory(dst, src, count);
61+
num_read += count;
62+
self.pos += count;
63+
},
64+
None => (),
65+
};
66+
if num_read == buf.len() || self.closed {
67+
break;
68+
}
69+
self.pos = 0;
70+
self.buf = self.port.try_recv();
71+
self.closed = self.buf.is_none();
72+
}
73+
if self.closed && num_read == 0 {
74+
io::io_error::cond.raise(io::standard_error(io::EndOfFile));
75+
None
76+
} else {
77+
Some(num_read)
78+
}
79+
}
2380

24-
fn eof(&mut self) -> bool { fail!() }
81+
fn eof(&mut self) -> bool { self.closed }
2582
}
2683

27-
pub struct ChanWriter<C>;
84+
/// Allows writing to a chan.
85+
///
86+
/// # Example
87+
///
88+
/// ```
89+
/// let writer = ChanWriter::new(chan);
90+
/// writer.write("hello, world".as_bytes());
91+
/// ```
92+
pub struct ChanWriter<C> {
93+
chan: C,
94+
}
2895

29-
impl<C: GenericChan<~[u8]>> ChanWriter<C> {
30-
pub fn new(_chan: C) -> ChanWriter<C> { fail!() }
96+
impl<C: GenericSmartChan<~[u8]>> ChanWriter<C> {
97+
pub fn new(chan: C) -> ChanWriter<C> {
98+
ChanWriter { chan: chan }
99+
}
31100
}
32101

33-
impl<C: GenericChan<~[u8]>> Writer for ChanWriter<C> {
34-
fn write(&mut self, _buf: &[u8]) { fail!() }
102+
impl<C: GenericSmartChan<~[u8]>> Writer for ChanWriter<C> {
103+
fn write(&mut self, buf: &[u8]) {
104+
if !self.chan.try_send(buf.to_owned()) {
105+
io::io_error::cond.raise(io::IoError {
106+
kind: io::BrokenPipe,
107+
desc: "Pipe closed",
108+
detail: None
109+
});
110+
}
111+
}
35112
}
36113

37114
pub struct ReaderPort<R>;
@@ -55,3 +132,87 @@ impl<W: Writer> WriterChan<W> {
55132
impl<W: Writer> GenericChan<~[u8]> for WriterChan<W> {
56133
fn send(&self, _x: ~[u8]) { fail!() }
57134
}
135+
136+
137+
#[cfg(test)]
138+
mod test {
139+
use prelude::*;
140+
use super::*;
141+
use io;
142+
use comm;
143+
use task;
144+
145+
#[test]
146+
fn test_port_reader() {
147+
let (port, chan) = comm::stream();
148+
do task::spawn {
149+
chan.send(~[1u8, 2u8]);
150+
chan.send(~[]);
151+
chan.send(~[3u8, 4u8]);
152+
chan.send(~[5u8, 6u8]);
153+
chan.send(~[7u8, 8u8]);
154+
}
155+
156+
let mut reader = PortReader::new(port);
157+
let mut buf = ~[0u8, ..3];
158+
159+
assert_eq!(false, reader.eof());
160+
161+
assert_eq!(Some(0), reader.read(~[]));
162+
assert_eq!(false, reader.eof());
163+
164+
assert_eq!(Some(3), reader.read(buf));
165+
assert_eq!(false, reader.eof());
166+
assert_eq!(~[1,2,3], buf);
167+
168+
assert_eq!(Some(3), reader.read(buf));
169+
assert_eq!(false, reader.eof());
170+
assert_eq!(~[4,5,6], buf);
171+
172+
assert_eq!(Some(2), reader.read(buf));
173+
assert_eq!(~[7,8,6], buf);
174+
assert_eq!(true, reader.eof());
175+
176+
let mut err = None;
177+
let result = io::io_error::cond.trap(|io::standard_error(k, _, _)| {
178+
err = Some(k)
179+
}).inside(|| {
180+
reader.read(buf)
181+
});
182+
assert_eq!(Some(io::EndOfFile), err);
183+
assert_eq!(None, result);
184+
assert_eq!(true, reader.eof());
185+
assert_eq!(~[7,8,6], buf);
186+
187+
// Ensure it continues to fail in the same way.
188+
err = None;
189+
let result = io::io_error::cond.trap(|io::standard_error(k, _, _)| {
190+
err = Some(k)
191+
}).inside(|| {
192+
reader.read(buf)
193+
});
194+
assert_eq!(Some(io::EndOfFile), err);
195+
assert_eq!(None, result);
196+
assert_eq!(true, reader.eof());
197+
assert_eq!(~[7,8,6], buf);
198+
}
199+
200+
#[test]
201+
fn test_chan_writer() {
202+
let (port, chan) = comm::stream();
203+
let mut writer = ChanWriter::new(chan);
204+
writer.write_be_u32(42);
205+
206+
let wanted = ~[0u8, 0u8, 0u8, 42u8];
207+
let got = do task::try { port.recv() }.unwrap();
208+
assert_eq!(wanted, got);
209+
210+
let mut err = None;
211+
io::io_error::cond.trap(|io::IoError { kind, .. } | {
212+
err = Some(kind)
213+
}).inside(|| {
214+
writer.write_u8(1)
215+
});
216+
assert_eq!(Some(io::BrokenPipe), err);
217+
}
218+
}

branches/try/src/libsyntax/ext/quote.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,78 +119,78 @@ pub mod rt {
119119
impl<'self> ToSource for &'self str {
120120
fn to_source(&self) -> @str {
121121
let lit = dummy_spanned(ast::lit_str(self.to_managed(), ast::CookedStr));
122-
pprust::lit_to_str(@lit).to_managed()
122+
pprust::lit_to_str(&lit).to_managed()
123123
}
124124
}
125125

126126
impl ToSource for int {
127127
fn to_source(&self) -> @str {
128128
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i));
129-
pprust::lit_to_str(@lit).to_managed()
129+
pprust::lit_to_str(&lit).to_managed()
130130
}
131131
}
132132

133133
impl ToSource for i8 {
134134
fn to_source(&self) -> @str {
135135
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i8));
136-
pprust::lit_to_str(@lit).to_managed()
136+
pprust::lit_to_str(&lit).to_managed()
137137
}
138138
}
139139

140140
impl ToSource for i16 {
141141
fn to_source(&self) -> @str {
142142
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i16));
143-
pprust::lit_to_str(@lit).to_managed()
143+
pprust::lit_to_str(&lit).to_managed()
144144
}
145145
}
146146

147147

148148
impl ToSource for i32 {
149149
fn to_source(&self) -> @str {
150150
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i32));
151-
pprust::lit_to_str(@lit).to_managed()
151+
pprust::lit_to_str(&lit).to_managed()
152152
}
153153
}
154154

155155
impl ToSource for i64 {
156156
fn to_source(&self) -> @str {
157157
let lit = dummy_spanned(ast::lit_int(*self as i64, ast::ty_i64));
158-
pprust::lit_to_str(@lit).to_managed()
158+
pprust::lit_to_str(&lit).to_managed()
159159
}
160160
}
161161

162162
impl ToSource for uint {
163163
fn to_source(&self) -> @str {
164164
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u));
165-
pprust::lit_to_str(@lit).to_managed()
165+
pprust::lit_to_str(&lit).to_managed()
166166
}
167167
}
168168

169169
impl ToSource for u8 {
170170
fn to_source(&self) -> @str {
171171
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u8));
172-
pprust::lit_to_str(@lit).to_managed()
172+
pprust::lit_to_str(&lit).to_managed()
173173
}
174174
}
175175

176176
impl ToSource for u16 {
177177
fn to_source(&self) -> @str {
178178
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u16));
179-
pprust::lit_to_str(@lit).to_managed()
179+
pprust::lit_to_str(&lit).to_managed()
180180
}
181181
}
182182

183183
impl ToSource for u32 {
184184
fn to_source(&self) -> @str {
185185
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u32));
186-
pprust::lit_to_str(@lit).to_managed()
186+
pprust::lit_to_str(&lit).to_managed()
187187
}
188188
}
189189

190190
impl ToSource for u64 {
191191
fn to_source(&self) -> @str {
192192
let lit = dummy_spanned(ast::lit_uint(*self as u64, ast::ty_u64));
193-
pprust::lit_to_str(@lit).to_managed()
193+
pprust::lit_to_str(&lit).to_managed()
194194
}
195195
}
196196

branches/try/src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ pub fn print_meta_item(s: @ps, item: &ast::MetaItem) {
19021902
ast::MetaNameValue(name, value) => {
19031903
word_space(s, name);
19041904
word_space(s, "=");
1905-
print_literal(s, @value);
1905+
print_literal(s, &value);
19061906
}
19071907
ast::MetaList(name, ref items) => {
19081908
word(s.s, name);

0 commit comments

Comments
 (0)