Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 7 pull requests #50345

Merged
merged 22 commits into from
Apr 30, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
256096d
Make Vec::new const
mark-i-m Apr 25, 2018
a2105b8
make RawVec::empty const
mark-i-m Apr 25, 2018
20ef0e0
make Vec::new const :P
mark-i-m Apr 26, 2018
c122b3a
not insta-stable
mark-i-m Apr 27, 2018
0212e02
feature on test
mark-i-m Apr 29, 2018
368fe37
Add more links in panic docs
Pazzaz Apr 29, 2018
269d279
Fix some broken links in docs.
ehuss Apr 29, 2018
e5280e4
use const trick
mark-i-m Apr 29, 2018
0a6e91b
Add a few more tests for proc macro feature gating
petrochenkov Apr 29, 2018
f9f9923
heh, logic is hard
mark-i-m Apr 29, 2018
cc53db8
Correct unused field warning on &struct match
varkor Apr 29, 2018
8e8fe90
Correct unused field warning on box struct match
varkor Apr 29, 2018
2eb8343
Correct unused field warning on struct match container patterns
varkor Apr 30, 2018
bd4ebf2
check that #[used] is used only on statics
japaric Apr 30, 2018
21941c8
Update Cargo to 2018-04-28 122fd5be5201913d42e219e132d6569493583bca
SimonSapin Apr 30, 2018
b88c152
Rollup merge of #50233 - mark-i-m:const_vec, r=kennytm
kennytm Apr 30, 2018
1308d99
Rollup merge of #50312 - Pazzaz:master, r=GuillaumeGomez
kennytm Apr 30, 2018
b239293
Rollup merge of #50316 - ehuss:fix-doc-links, r=frewsxcv
kennytm Apr 30, 2018
30c990b
Rollup merge of #50325 - petrochenkov:pmgate, r=alexcrichton
kennytm Apr 30, 2018
cbbdf99
Rollup merge of #50327 - varkor:match-unused-struct-field, r=estebank
kennytm Apr 30, 2018
909ba8a
Rollup merge of #50330 - japaric:used, r=nagisa
kennytm Apr 30, 2018
6166f20
Rollup merge of #50344 - SimonSapin:cargo, r=alexcrichton
kennytm Apr 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Make Vec::new const
  • Loading branch information
mark-i-m committed Apr 25, 2018
commit 256096da9ee680366b839f912e8d3ecccc0da033
16 changes: 16 additions & 0 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ impl<T, A: Alloc> RawVec<T, A> {
}
}

/// Like `empty` but parametrized over the choice of allocator for the returned `RawVec`.
pub const fn empty_in(a: A) -> Self {
// Unique::empty() doubles as "unallocated" and "zero-sized allocation"
RawVec {
ptr: Unique::empty(),
cap: 0,
a,
}
}

/// Like `with_capacity` but parameterized over the choice of
/// allocator for the returned RawVec.
#[inline]
Expand Down Expand Up @@ -124,6 +134,12 @@ impl<T> RawVec<T, Global> {
Self::new_in(Global)
}

/// Create a `RawVec` with capcity 0 (on the system heap), regardless of `T`, without
/// allocating.
pub fn empty() -> Self {
Self::empty_in(Global)
}

/// Creates a RawVec (on the system heap) with exactly the
/// capacity and alignment requirements for a `[T; cap]`. This is
/// equivalent to calling RawVec::new when `cap` is 0 or T is
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl<T> Vec<T> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn new() -> Vec<T> {
Vec {
buf: RawVec::new(),
buf: RawVec::empty(),
len: 0,
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2551,10 +2551,9 @@ impl<T: Sized> Unique<T> {
/// This is useful for initializing types which lazily allocate, like
/// `Vec::new` does.
// FIXME: rename to dangling() to match NonNull?
pub fn empty() -> Self {
pub const fn empty() -> Self {
unsafe {
let ptr = mem::align_of::<T>() as *mut T;
Unique::new_unchecked(ptr)
Unique::new_unchecked(mem::align_of::<T>() as *mut T)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/run-pass/vec-const-new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2012 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// Test that Vec::new() can be used for constants

const MY_VEC: Vec<usize> = Vec::new();

pub fn main() {}