Skip to content

Commit c0fdfd6

Browse files
committed
---
yaml --- r: 152142 b: refs/heads/try2 c: 36c2c56 h: refs/heads/master v: v3
1 parent 14d7dad commit c0fdfd6

File tree

19 files changed

+181
-66
lines changed

19 files changed

+181
-66
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: 8e8f6a0372576b35a21d4785b2e4291a13fccf02
8+
refs/heads/try2: 36c2c56277b368e5200fc4a7e8ed6752562ce030
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ config.mk
6060
/rt/
6161
/rustllvm/
6262
/test/
63-
/build
6463
/inst/
6564
/mingw-build/
6665
src/.DS_Store

branches/try2/src/doc/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ let ys = xs;
11121112
11131113
xs = Nil;
11141114
1115-
// `xs` can't be used again
1115+
// `xs` can be used again
11161116
~~~
11171117

11181118
A destructor call will only occur for a variable that has not been moved from,

branches/try2/src/liballoc/arc.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ impl<T: Send + Share + Clone> Arc<T> {
152152
#[inline]
153153
#[experimental]
154154
pub fn make_unique<'a>(&'a mut self) -> &'a mut T {
155-
if self.inner().strong.load(atomics::SeqCst) != 1 {
155+
// Note that we hold a strong reference, which also counts as
156+
// a weak reference, so we only clone if there is an
157+
// additional reference of either kind.
158+
if self.inner().strong.load(atomics::SeqCst) != 1 ||
159+
self.inner().weak.load(atomics::SeqCst) != 1 {
156160
*self = Arc::new(self.deref().clone())
157161
}
158162
// This unsafety is ok because we're guaranteed that the pointer
@@ -356,6 +360,20 @@ mod tests {
356360
assert!(*cow1 == *cow2);
357361
}
358362

363+
#[test]
364+
fn test_cowarc_clone_weak() {
365+
let mut cow0 = Arc::new(75u);
366+
let cow1_weak = cow0.downgrade();
367+
368+
assert!(75 == *cow0);
369+
assert!(75 == *cow1_weak.upgrade().unwrap());
370+
371+
*cow0.make_unique() += 1;
372+
373+
assert!(76 == *cow0);
374+
assert!(cow1_weak.upgrade().is_none());
375+
}
376+
359377
#[test]
360378
fn test_live() {
361379
let x = Arc::new(5);

branches/try2/src/liballoc/rc.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ impl<T> Rc<T> {
8686
}
8787
}
8888

89+
impl<T: Clone> Rc<T> {
90+
/// Acquires a mutable pointer to the inner contents by guaranteeing that
91+
/// the reference count is one (no sharing is possible).
92+
///
93+
/// This is also referred to as a copy-on-write operation because the inner
94+
/// data is cloned if the reference count is greater than one.
95+
#[inline]
96+
#[experimental]
97+
pub fn make_unique<'a>(&'a mut self) -> &'a mut T {
98+
// Note that we hold a strong reference, which also counts as
99+
// a weak reference, so we only clone if there is an
100+
// additional reference of either kind.
101+
if self.strong() != 1 || self.weak() != 1 {
102+
*self = Rc::new(self.deref().clone())
103+
}
104+
// This unsafety is ok because we're guaranteed that the pointer
105+
// returned is the *only* pointer that will ever be returned to T. Our
106+
// reference count is guaranteed to be 1 at this point, and we required
107+
// the Rc itself to be `mut`, so we're returning the only possible
108+
// reference to the inner data.
109+
let inner = unsafe { &mut *self._ptr };
110+
&mut inner.value
111+
}
112+
}
113+
89114
impl<T> Deref<T> for Rc<T> {
90115
/// Borrow the value contained in the reference-counted box
91116
#[inline(always)]
@@ -234,6 +259,7 @@ impl<T> RcBoxPtr<T> for Weak<T> {
234259
}
235260

236261
#[cfg(test)]
262+
#[allow(experimental)]
237263
mod tests {
238264
use super::{Rc, Weak};
239265
use std::cell::RefCell;
@@ -304,4 +330,66 @@ mod tests {
304330

305331
// hopefully we don't double-free (or leak)...
306332
}
333+
334+
#[test]
335+
fn test_cowrc_clone_make_unique() {
336+
let mut cow0 = Rc::new(75u);
337+
let mut cow1 = cow0.clone();
338+
let mut cow2 = cow1.clone();
339+
340+
assert!(75 == *cow0.make_unique());
341+
assert!(75 == *cow1.make_unique());
342+
assert!(75 == *cow2.make_unique());
343+
344+
*cow0.make_unique() += 1;
345+
*cow1.make_unique() += 2;
346+
*cow2.make_unique() += 3;
347+
348+
assert!(76 == *cow0);
349+
assert!(77 == *cow1);
350+
assert!(78 == *cow2);
351+
352+
// none should point to the same backing memory
353+
assert!(*cow0 != *cow1);
354+
assert!(*cow0 != *cow2);
355+
assert!(*cow1 != *cow2);
356+
}
357+
358+
#[test]
359+
fn test_cowrc_clone_unique2() {
360+
let mut cow0 = Rc::new(75u);
361+
let cow1 = cow0.clone();
362+
let cow2 = cow1.clone();
363+
364+
assert!(75 == *cow0);
365+
assert!(75 == *cow1);
366+
assert!(75 == *cow2);
367+
368+
*cow0.make_unique() += 1;
369+
370+
assert!(76 == *cow0);
371+
assert!(75 == *cow1);
372+
assert!(75 == *cow2);
373+
374+
// cow1 and cow2 should share the same contents
375+
// cow0 should have a unique reference
376+
assert!(*cow0 != *cow1);
377+
assert!(*cow0 != *cow2);
378+
assert!(*cow1 == *cow2);
379+
}
380+
381+
#[test]
382+
fn test_cowrc_clone_weak() {
383+
let mut cow0 = Rc::new(75u);
384+
let cow1_weak = cow0.downgrade();
385+
386+
assert!(75 == *cow0);
387+
assert!(75 == *cow1_weak.upgrade().unwrap());
388+
389+
*cow0.make_unique() += 1;
390+
391+
assert!(76 == *cow0);
392+
assert!(cow1_weak.upgrade().is_none());
393+
}
394+
307395
}

branches/try2/src/libcollections/hashmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,8 +2013,8 @@ mod test_map {
20132013

20142014
let map_str = format!("{}", map);
20152015

2016-
assert!(map_str == "{1: 2, 3: 4}".to_owned() || map_str == "{3: 4, 1: 2}".to_owned());
2017-
assert_eq!(format!("{}", empty), "{}".to_owned());
2016+
assert!(map_str == "{1: 2, 3: 4}".to_string() || map_str == "{3: 4, 1: 2}".to_string());
2017+
assert_eq!(format!("{}", empty), "{}".to_string());
20182018
}
20192019

20202020
#[test]

branches/try2/src/libcollections/treemap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ mod test_treemap {
13661366

13671367
let map_str = format!("{}", map);
13681368

1369-
assert!(map_str == "{1: 2, 3: 4}".to_owned());
1370-
assert_eq!(format!("{}", empty), "{}".to_owned());
1369+
assert!(map_str == "{1: 2, 3: 4}".to_string());
1370+
assert_eq!(format!("{}", empty), "{}".to_string());
13711371
}
13721372

13731373
#[test]
@@ -1776,7 +1776,7 @@ mod test_set {
17761776

17771777
let set_str = format!("{}", set);
17781778

1779-
assert!(set_str == "{1, 2}".to_owned());
1780-
assert_eq!(format!("{}", empty), "{}".to_owned());
1779+
assert!(set_str == "{1, 2}".to_string());
1780+
assert_eq!(format!("{}", empty), "{}".to_string());
17811781
}
17821782
}

branches/try2/src/libdebug/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ fn test_repr() {
613613
fn exact_test<T>(t: &T, e:&str) {
614614
let mut m = io::MemWriter::new();
615615
write_repr(&mut m as &mut io::Writer, t).unwrap();
616-
let s = str::from_utf8(m.unwrap().as_slice()).unwrap().to_owned();
616+
let s = str::from_utf8(m.unwrap().as_slice()).unwrap().to_string();
617617
assert_eq!(s.as_slice(), e);
618618
}
619619

branches/try2/src/libgreen/sched.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -977,8 +977,8 @@ impl ClosureConverter for UnsafeTaskReceiver {
977977
// worry there.
978978
#[cfg(windows)]
979979
fn new_sched_rng() -> XorShiftRng {
980-
use std::rand::OSRng;
981-
match OSRng::new() {
980+
use std::rand::OsRng;
981+
match OsRng::new() {
982982
Ok(mut r) => r.gen(),
983983
Err(e) => {
984984
rtabort!("sched: failed to create seeded RNG: {}", e)

branches/try2/src/librand/isaac.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static RAND_SIZE: u32 = 1 << RAND_SIZE_LEN;
2424
///
2525
/// The ISAAC algorithm is generally accepted as suitable for
2626
/// cryptographic purposes, but this implementation has not be
27-
/// verified as such. Prefer a generator like `OSRng` that defers to
27+
/// verified as such. Prefer a generator like `OsRng` that defers to
2828
/// the operating system for cases that need high security.
2929
///
3030
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number
@@ -231,7 +231,7 @@ static RAND_SIZE_64: uint = 1 << RAND_SIZE_64_LEN;
231231
///
232232
/// The ISAAC algorithm is generally accepted as suitable for
233233
/// cryptographic purposes, but this implementation has not be
234-
/// verified as such. Prefer a generator like `OSRng` that defers to
234+
/// verified as such. Prefer a generator like `OsRng` that defers to
235235
/// the operating system for cases that need high security.
236236
///
237237
/// [1]: Bob Jenkins, [*ISAAC: A fast cryptographic random number

branches/try2/src/librand/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub trait SeedableRng<Seed>: Rng {
333333
///
334334
/// The Xorshift algorithm is not suitable for cryptographic purposes
335335
/// but is very fast. If you do not know for sure that it fits your
336-
/// requirements, use a more secure one such as `IsaacRng` or `OSRng`.
336+
/// requirements, use a more secure one such as `IsaacRng` or `OsRng`.
337337
///
338338
/// [1]: Marsaglia, George (July 2003). ["Xorshift
339339
/// RNGs"](http://www.jstatsoft.org/v08/i14/paper). *Journal of

branches/try2/src/libregex/re.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,13 @@ impl<'t> Replacer for NoExpand<'t> {
573573

574574
impl<'t> Replacer for &'t str {
575575
fn reg_replace<'a>(&'a mut self, caps: &Captures) -> MaybeOwned<'a> {
576-
Owned(caps.expand(*self).into_owned())
576+
Owned(caps.expand(*self))
577577
}
578578
}
579579

580580
impl<'a> Replacer for |&Captures|: 'a -> String {
581581
fn reg_replace<'r>(&'r mut self, caps: &Captures) -> MaybeOwned<'r> {
582-
Owned((*self)(caps).into_owned())
582+
Owned((*self)(caps))
583583
}
584584
}
585585

branches/try2/src/librustdoc/html/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl fmt::Show for clean::Type {
356356
}
357357
}
358358
}
359-
ret.into_owned()
359+
ret
360360
})
361361
}
362362
clean::Proc(ref decl) => {

branches/try2/src/libstd/rand/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ after generating 32 KiB of random data.
3232
# Cryptographic security
3333
3434
An application that requires an entropy source for cryptographic purposes
35-
must use `OSRng`, which reads randomness from the source that the operating
35+
must use `OsRng`, which reads randomness from the source that the operating
3636
system provides (e.g. `/dev/urandom` on Unixes or `CryptGenRandom()` on Windows).
3737
The other random number generators provided by this module are not suitable
3838
for such purposes.
@@ -91,7 +91,7 @@ use IsaacWordRng = core_rand::Isaac64Rng;
9191
pub use core_rand::{Rand, Rng, SeedableRng, Open01, Closed01};
9292
pub use core_rand::{XorShiftRng, IsaacRng, Isaac64Rng};
9393
pub use core_rand::{distributions, reseeding};
94-
pub use rand::os::OSRng;
94+
pub use rand::os::OsRng;
9595

9696
pub mod os;
9797
pub mod reader;
@@ -113,7 +113,7 @@ impl StdRng {
113113
/// Reading the randomness from the OS may fail, and any error is
114114
/// propagated via the `IoResult` return value.
115115
pub fn new() -> IoResult<StdRng> {
116-
OSRng::new().map(|mut r| StdRng { rng: r.gen() })
116+
OsRng::new().map(|mut r| StdRng { rng: r.gen() })
117117
}
118118
}
119119

@@ -151,7 +151,7 @@ impl<'a> SeedableRng<&'a [uint]> for StdRng {
151151
/// This will read randomness from the operating system to seed the
152152
/// generator.
153153
pub fn weak_rng() -> XorShiftRng {
154-
match OSRng::new() {
154+
match OsRng::new() {
155155
Ok(mut r) => r.gen(),
156156
Err(e) => fail!("weak_rng: failed to create seeded RNG: {}", e)
157157
}
@@ -467,12 +467,12 @@ mod bench {
467467

468468
use self::test::Bencher;
469469
use super::{XorShiftRng, StdRng, IsaacRng, Isaac64Rng, Rng, RAND_BENCH_N};
470-
use super::{OSRng, weak_rng};
470+
use super::{OsRng, weak_rng};
471471
use mem::size_of;
472472

473473
#[bench]
474474
fn rand_xorshift(b: &mut Bencher) {
475-
let mut rng: XorShiftRng = OSRng::new().unwrap().gen();
475+
let mut rng: XorShiftRng = OsRng::new().unwrap().gen();
476476
b.iter(|| {
477477
for _ in range(0, RAND_BENCH_N) {
478478
rng.gen::<uint>();
@@ -483,7 +483,7 @@ mod bench {
483483

484484
#[bench]
485485
fn rand_isaac(b: &mut Bencher) {
486-
let mut rng: IsaacRng = OSRng::new().unwrap().gen();
486+
let mut rng: IsaacRng = OsRng::new().unwrap().gen();
487487
b.iter(|| {
488488
for _ in range(0, RAND_BENCH_N) {
489489
rng.gen::<uint>();
@@ -494,7 +494,7 @@ mod bench {
494494

495495
#[bench]
496496
fn rand_isaac64(b: &mut Bencher) {
497-
let mut rng: Isaac64Rng = OSRng::new().unwrap().gen();
497+
let mut rng: Isaac64Rng = OsRng::new().unwrap().gen();
498498
b.iter(|| {
499499
for _ in range(0, RAND_BENCH_N) {
500500
rng.gen::<uint>();

0 commit comments

Comments
 (0)