From bed5a7d92a90f067e933fb7e4f2d656330f88f11 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 25 Oct 2014 18:33:56 -0700 Subject: Add MemWriter::from_vec --- src/libstd/io/mem.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/io/mem.rs b/src/libstd/io/mem.rs index f86ae05d623..dd4a3e05935 100644 --- a/src/libstd/io/mem.rs +++ b/src/libstd/io/mem.rs @@ -22,7 +22,7 @@ use slice; use slice::AsSlice; use vec::Vec; -static BUF_CAPACITY: uint = 128; +const BUF_CAPACITY: uint = 128; fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult { // compute offset as signed and clamp to prevent overflow @@ -71,7 +71,12 @@ impl MemWriter { /// the internal buffer. #[inline] pub fn with_capacity(n: uint) -> MemWriter { - MemWriter { buf: Vec::with_capacity(n) } + MemWriter::from_vec(Vec::with_capacity(n)) + } + /// Create a new `MemWriter` that will append to an existing `Vec`. + #[inline] + pub fn from_vec(buf: Vec) -> MemWriter { + MemWriter { buf: buf } } /// Acquires an immutable reference to the underlying buffer of this -- cgit 1.4.1-3-g733a5 From 30403204d695b687cc264c875eae829ae9368937 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sat, 25 Oct 2014 23:10:16 -0400 Subject: Fix spelling mistakes in comments. --- src/etc/emacs/rust-mode-tests.el | 2 +- src/libcollections/btree/map.rs | 4 ++-- src/libcollections/btree/node.rs | 18 +++++++++--------- src/librand/chacha.rs | 2 +- src/librustc/middle/traits/select.rs | 2 +- src/librustc/middle/trans/expr.rs | 4 ++-- src/librustc/middle/ty.rs | 4 ++-- src/librustc/middle/typeck/check/regionck.rs | 2 +- src/libstd/collections/hashmap/set.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/test/run-pass/dst-coercions.rs | 2 +- src/test/run-pass/realloc-16687.rs | 2 +- src/test/run-pass/vec-dst.rs | 2 +- 13 files changed, 24 insertions(+), 24 deletions(-) (limited to 'src/libstd') diff --git a/src/etc/emacs/rust-mode-tests.el b/src/etc/emacs/rust-mode-tests.el index 1b6794e77f9..f255dbf1507 100644 --- a/src/etc/emacs/rust-mode-tests.el +++ b/src/etc/emacs/rust-mode-tests.el @@ -376,7 +376,7 @@ fn bar( a:int, -> int { } -fn baz( a:int, // shoudl work with a comment here +fn baz( a:int, // should work with a comment here b:char) -> int { } diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index dbbff61b8dd..77fb6d4a120 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -41,10 +41,10 @@ use ringbuf::RingBuf; /// the BST strategy. /// /// A B-Tree instead makes each node contain B-1 to 2B-1 elements in a contiguous array. By doing -/// this, we reduce the number of allocations by a factor of B, and improve cache effeciency in +/// this, we reduce the number of allocations by a factor of B, and improve cache efficiency in /// searches. However, this does mean that searches will have to do *more* comparisons on average. /// The precise number of comparisons depends on the node search strategy used. For optimal cache -/// effeciency, one could search the nodes linearly. For optimal comparisons, one could search +/// efficiency, one could search the nodes linearly. For optimal comparisons, one could search /// the node using binary search. As a compromise, one could also perform a linear search /// that initially only checks every ith element for some choice of i. /// diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index e30b29f8767..4da362952b6 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -53,7 +53,7 @@ pub struct Node { // hard. For now, we accept this cost in the name of correctness and simplicity. // // As a compromise, keys and vals could be merged into one Vec<(K, V)>, which would shave - // off 3 words, but possibly hurt our cache effeciency during search, which only cares about + // off 3 words, but possibly hurt our cache efficiency during search, which only cares about // keys. This would also avoid the Zip we use in our iterator implementations. This is // probably worth investigating. // @@ -72,7 +72,7 @@ impl Node { /// `GoDown` will be yielded with the index of the subtree the key must lie in. pub fn search(&self, key: &K) -> SearchResult { // FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V). - // For the B configured as of this writing (B = 6), binary search was *singnificantly* + // For the B configured as of this writing (B = 6), binary search was *significantly* // worse for uints. self.search_linear(key) } @@ -375,7 +375,7 @@ impl Node { } } - /// Steal! Stealing is roughly analagous to a binary tree rotation. + /// Steal! Stealing is roughly analogous to a binary tree rotation. /// In this case, we're "rotating" right. unsafe fn steal_to_left(&mut self, underflowed_child_index: uint) { // Take the biggest stuff off left @@ -387,7 +387,7 @@ impl Node { } }; - // Swap the parent's seperating key-value pair with left's + // Swap the parent's separating key-value pair with left's self.unsafe_swap(underflowed_child_index - 1, &mut key, &mut val); // Put them at the start of right @@ -402,7 +402,7 @@ impl Node { } } - /// Steal! Stealing is roughly analagous to a binary tree rotation. + /// Steal! Stealing is roughly analogous to a binary tree rotation. /// In this case, we're "rotating" left. unsafe fn steal_to_right(&mut self, underflowed_child_index: uint) { // Take the smallest stuff off right @@ -414,7 +414,7 @@ impl Node { } }; - // Swap the parent's seperating key-value pair with right's + // Swap the parent's separating key-value pair with right's self.unsafe_swap(underflowed_child_index, &mut key, &mut val); // Put them at the end of left @@ -430,9 +430,9 @@ impl Node { } /// Merge! Left and right will be smooshed into one node, along with the key-value - /// pair that seperated them in their parent. + /// pair that separated them in their parent. unsafe fn merge_children(&mut self, left_index: uint) { - // Permanently remove right's index, and the key-value pair that seperates + // Permanently remove right's index, and the key-value pair that separates // left and right let (key, val, right) = { match (self.keys.remove(left_index), @@ -448,7 +448,7 @@ impl Node { left.absorb(key, val, right); } - /// Take all the values from right, seperated by the given key and value + /// Take all the values from right, separated by the given key and value fn absorb(&mut self, key: K, val: V, right: Node) { // Just as a sanity check, make sure we can fit this guy in debug_assert!(self.len() + right.len() <= self.capacity()) diff --git a/src/librand/chacha.rs b/src/librand/chacha.rs index 83d03bb265e..97e68bcbb2c 100644 --- a/src/librand/chacha.rs +++ b/src/librand/chacha.rs @@ -173,7 +173,7 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng { fn reseed(&mut self, seed: &'a [u32]) { // reset state self.init(&[0u32, ..KEY_WORDS]); - // set key inplace + // set key in place let key = self.state.slice_mut(4, 4+KEY_WORDS); for (k, s) in key.iter_mut().zip(seed.iter()) { *k = *s; diff --git a/src/librustc/middle/traits/select.rs b/src/librustc/middle/traits/select.rs index 23257912b82..998a9164647 100644 --- a/src/librustc/middle/traits/select.rs +++ b/src/librustc/middle/traits/select.rs @@ -211,7 +211,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { // can be applied to particular types. It skips the "confirmation" // step and hence completely ignores output type parameters. // - // The result is "true" if the obliation *may* hold and "false" if + // The result is "true" if the obligation *may* hold and "false" if // we can be sure it does not. pub fn evaluate_obligation_intercrate(&mut self, diff --git a/src/librustc/middle/trans/expr.rs b/src/librustc/middle/trans/expr.rs index 834441d4430..cec96b13fbe 100644 --- a/src/librustc/middle/trans/expr.rs +++ b/src/librustc/middle/trans/expr.rs @@ -2117,7 +2117,7 @@ fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, deref_owned_pointer(bcx, expr, datum, content_ty) } else { // A fat pointer and an opened DST value have the same - // represenation just different types. Since there is no + // representation just different types. Since there is no // temporary for `*e` here (because it is unsized), we cannot // emulate the sized object code path for running drop glue and // free. Instead, we schedule cleanup for `e`, turning it into @@ -2142,7 +2142,7 @@ fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, // owner (or, in the case of *T, by the user). DatumBlock::new(bcx, Datum::new(ptr, content_ty, LvalueExpr)) } else { - // A fat pointer and an opened DST value have the same represenation + // A fat pointer and an opened DST value have the same representation // just different types. DatumBlock::new(bcx, Datum::new(datum.val, ty::mk_open(bcx.tcx(), content_ty), diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 8c602548f33..1ce567e6329 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -3605,7 +3605,7 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind { // Special case: A unit like struct's constructor must be called without () at the // end (like `UnitStruct`) which means this is an ExprPath to a DefFn. But in case - // of unit structs this is should not be interpretet as function pointer but as + // of unit structs this is should not be interpreted as function pointer but as // call to the constructor. def::DefFn(_, _, true) => RvalueDpsExpr, @@ -5409,7 +5409,7 @@ impl BorrowKind { MutBorrow => ast::MutMutable, ImmBorrow => ast::MutImmutable, - // We have no type correponding to a unique imm borrow, so + // We have no type corresponding to a unique imm borrow, so // use `&mut`. It gives all the capabilities of an `&uniq` // and hence is a safe "over approximation". UniqueImmBorrow => ast::MutMutable, diff --git a/src/librustc/middle/typeck/check/regionck.rs b/src/librustc/middle/typeck/check/regionck.rs index 31fe30fc9f8..c49246b5c54 100644 --- a/src/librustc/middle/typeck/check/regionck.rs +++ b/src/librustc/middle/typeck/check/regionck.rs @@ -1675,7 +1675,7 @@ fn link_reborrowed_region(rcx: &Rcx, // // If mutability was inferred from an upvar, we may be // forced to revisit this decision later if processing - // another borrow or nested closure ends up coverting the + // another borrow or nested closure ends up converting the // upvar borrow kind to mutable/unique. Record the // information needed to perform the recursive link in the // maybe link map. diff --git a/src/libstd/collections/hashmap/set.rs b/src/libstd/collections/hashmap/set.rs index dde1f27c9a3..ca954679c1c 100644 --- a/src/libstd/collections/hashmap/set.rs +++ b/src/libstd/collections/hashmap/set.rs @@ -186,7 +186,7 @@ impl, S, H: Hasher> HashSet { /// # Example /// /// This is a slightly silly example where we define the number's - /// parity as the equivilance class. It is important that the + /// parity as the equivalance class. It is important that the /// values hash the same, which is why we implement `Hash`. /// /// ``` diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 8eaee7282d1..c06feae6872 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -436,7 +436,7 @@ pub enum Stmt_ { /// Expr with trailing semi-colon (may have any type): StmtSemi(P, NodeId), - /// bool: is there a trailing sem-colon? + /// bool: is there a trailing semi-colon? StmtMac(Mac, bool), } diff --git a/src/test/run-pass/dst-coercions.rs b/src/test/run-pass/dst-coercions.rs index 1c9d5cd3afe..dbad546ce1a 100644 --- a/src/test/run-pass/dst-coercions.rs +++ b/src/test/run-pass/dst-coercions.rs @@ -28,7 +28,7 @@ pub fn main() { let x: *mut S = &mut S; - // Test we can chnage the mutability from mut to const. + // Test we can change the mutability from mut to const. let x: &T = &mut S; let x: *const T = &mut S; } diff --git a/src/test/run-pass/realloc-16687.rs b/src/test/run-pass/realloc-16687.rs index d8f48c7e662..966e34dfe49 100644 --- a/src/test/run-pass/realloc-16687.rs +++ b/src/test/run-pass/realloc-16687.rs @@ -30,7 +30,7 @@ unsafe fn test_triangle() -> bool { let ascend = ascend.as_mut_slice(); static ALIGN : uint = 1; - // Checks that `ascend` forms triangle of acending size formed + // Checks that `ascend` forms triangle of ascending size formed // from pairs of rows (where each pair of rows is equally sized), // and the elements of the triangle match their row-pair index. unsafe fn sanity_check(ascend: &[*mut u8]) { diff --git a/src/test/run-pass/vec-dst.rs b/src/test/run-pass/vec-dst.rs index 2fe8f4bdf01..11b58948e05 100644 --- a/src/test/run-pass/vec-dst.rs +++ b/src/test/run-pass/vec-dst.rs @@ -10,7 +10,7 @@ fn sub_expr() { // Test for a &[T] => &&[T] coercion in sub-expression position - // (surpisingly, this can cause errors which are not caused by either of: + // (surprisingly, this can cause errors which are not caused by either of: // `let x = vec.slice_mut(0, 2);` // `foo(vec.slice_mut(0, 2));` ). let mut vec: Vec = vec!(1, 2, 3, 4); -- cgit 1.4.1-3-g733a5 From b8c4eb3a4e3bf53b220578d69d76a5a2e7f414f5 Mon Sep 17 00:00:00 2001 From: Adolfo OchagavĂ­a Date: Mon, 27 Oct 2014 16:04:55 +0100 Subject: Fix undefined behavior in std::ascii Closes https://github.com/rust-lang/rust/issues/18314 --- src/libstd/ascii.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 07be15486fd..f7a84fc3478 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -247,8 +247,7 @@ impl OwnedAsciiCast for String { #[inline] unsafe fn into_ascii_nocheck(self) -> Vec { - let v: Vec = mem::transmute(self); - v.into_ascii_nocheck() + self.into_bytes().into_ascii_nocheck() } } @@ -260,7 +259,14 @@ impl OwnedAsciiCast for Vec { #[inline] unsafe fn into_ascii_nocheck(self) -> Vec { - mem::transmute(self) + let v = Vec::from_raw_parts(self.len(), + self.capacity(), + mem::transmute(self.as_ptr())); + + // We forget `self` to avoid freeing it at the end of the scope + // Otherwise, the returned `Vec` would point to freed memory + mem::forget(self); + v } } @@ -338,7 +344,16 @@ pub trait IntoBytes { impl IntoBytes for Vec { fn into_bytes(self) -> Vec { - unsafe { mem::transmute(self) } + unsafe { + let v = Vec::from_raw_parts(self.len(), + self.capacity(), + mem::transmute(self.as_ptr())); + + // We forget `self` to avoid freeing it at the end of the scope + // Otherwise, the returned `Vec` would point to freed memory + mem::forget(self); + v + } } } -- cgit 1.4.1-3-g733a5 From a33d7617c5b5d18ff495fe6aa9108aa13939a114 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 27 Oct 2014 09:11:07 -0700 Subject: Test fixes and rebase conflicts from rollup --- src/libcollections/vec.rs | 2 +- src/libstd/ascii.rs | 12 ++++++------ src/test/compile-fail/issue-17458.rs | 16 ++++++++++++++++ src/test/run-pass/issue-16668.rs | 2 ++ src/test/run-pass/issue-17458.rs | 15 --------------- 5 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 src/test/compile-fail/issue-17458.rs delete mode 100644 src/test/run-pass/issue-17458.rs (limited to 'src/libstd') diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index c57a465df37..765c9827cb7 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -231,7 +231,7 @@ impl Vec { /// } /// /// // Put everything back together into a Vec - /// let rebuilt = Vec::from_raw_parts(len, cap, p); + /// let rebuilt = Vec::from_raw_parts(p, len, cap); /// assert_eq!(rebuilt, vec![4i, 5i, 6i]); /// } /// } diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index f7a84fc3478..c2e88bfdbcf 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -259,9 +259,9 @@ impl OwnedAsciiCast for Vec { #[inline] unsafe fn into_ascii_nocheck(self) -> Vec { - let v = Vec::from_raw_parts(self.len(), - self.capacity(), - mem::transmute(self.as_ptr())); + let v = Vec::from_raw_parts(self.as_ptr() as *mut Ascii, + self.len(), + self.capacity()); // We forget `self` to avoid freeing it at the end of the scope // Otherwise, the returned `Vec` would point to freed memory @@ -345,9 +345,9 @@ pub trait IntoBytes { impl IntoBytes for Vec { fn into_bytes(self) -> Vec { unsafe { - let v = Vec::from_raw_parts(self.len(), - self.capacity(), - mem::transmute(self.as_ptr())); + let v = Vec::from_raw_parts(self.as_ptr() as *mut u8, + self.len(), + self.capacity()); // We forget `self` to avoid freeing it at the end of the scope // Otherwise, the returned `Vec` would point to freed memory diff --git a/src/test/compile-fail/issue-17458.rs b/src/test/compile-fail/issue-17458.rs new file mode 100644 index 00000000000..b1fbe6f5549 --- /dev/null +++ b/src/test/compile-fail/issue-17458.rs @@ -0,0 +1,16 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +static X: uint = 0 as *const uint as uint; +//~^ ERROR: can not cast a pointer to an integer in a constant expression + +fn main() { + assert_eq!(X, 0); +} diff --git a/src/test/run-pass/issue-16668.rs b/src/test/run-pass/issue-16668.rs index 1bfa79b8a11..b66fb4306d0 100644 --- a/src/test/run-pass/issue-16668.rs +++ b/src/test/run-pass/issue-16668.rs @@ -8,6 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-pretty + #![feature(unboxed_closures)] struct Parser<'a, I, O> { diff --git a/src/test/run-pass/issue-17458.rs b/src/test/run-pass/issue-17458.rs deleted file mode 100644 index a32a31e97e3..00000000000 --- a/src/test/run-pass/issue-17458.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2014 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -static X: uint = 0 as *const uint as uint; - -fn main() { - assert_eq!(X, 0); -} -- cgit 1.4.1-3-g733a5