提交 c2bacd2e 编写于 作者: B bors

auto merge of #8183 : omasanori/rust/migrate-new, r=sanxiyn

It seems that relatively new code uses `Foo::new()` instead of `Foo()` so I wrote a patch to migrate some structs to the former style.
Is it a right direction? If there are any guidelines not to use new()-style, could you add them to the [style guide](https://github.com/omasanori/rust/wiki/Note-style-guide)?
......@@ -67,17 +67,16 @@ pub struct Arena {
priv chunks: @mut MutList<Chunk>,
}
#[unsafe_destructor]
impl Drop for Arena {
fn drop(&self) {
unsafe {
destroy_chunk(&self.head);
do self.chunks.each |chunk| {
if !chunk.is_pod {
destroy_chunk(chunk);
}
true
};
impl Arena {
pub fn new() -> Arena {
Arena::new_with_size(32u)
}
pub fn new_with_size(initial_size: uint) -> Arena {
Arena {
head: chunk(initial_size, false),
pod_head: chunk(initial_size, true),
chunks: @mut MutNil,
}
}
}
......@@ -92,18 +91,21 @@ fn chunk(size: uint, is_pod: bool) -> Chunk {
}
}
pub fn arena_with_size(initial_size: uint) -> Arena {
Arena {
head: chunk(initial_size, false),
pod_head: chunk(initial_size, true),
chunks: @mut MutNil,
#[unsafe_destructor]
impl Drop for Arena {
fn drop(&self) {
unsafe {
destroy_chunk(&self.head);
do self.chunks.each |chunk| {
if !chunk.is_pod {
destroy_chunk(chunk);
}
true
};
}
}
}
pub fn Arena() -> Arena {
arena_with_size(32u)
}
#[inline]
fn round_up_to(base: uint, align: uint) -> uint {
(base + (align - 1)) & !(align - 1)
......@@ -276,7 +278,7 @@ pub fn alloc<'a, T>(&'a self, op: &fn() -> T) -> &'a T {
#[test]
fn test_arena_destructors() {
let arena = Arena();
let arena = Arena::new();
for i in range(0u, 10) {
// Arena allocate something with drop glue to make sure it
// doesn't leak.
......@@ -291,7 +293,7 @@ fn test_arena_destructors() {
#[should_fail]
#[ignore(cfg(windows))]
fn test_arena_destructors_fail() {
let arena = Arena();
let arena = Arena::new();
// Put some stuff in the arena.
for i in range(0u, 10) {
// Arena allocate something with drop glue to make sure it
......
......@@ -9,7 +9,7 @@
// except according to those terms.
extern mod extra;
use extra::arena;
use extra::arena::Arena;
enum Tree<'self> {
Nil,
......@@ -25,7 +25,7 @@ fn item_check(t: &Tree) -> int {
}
}
fn bottom_up_tree<'r>(arena: &'r arena::Arena, item: int, depth: int)
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int)
-> &'r Tree<'r> {
if depth > 0 {
return arena.alloc(
......@@ -57,7 +57,7 @@ fn main() {
max_depth = n;
}
let stretch_arena = arena::Arena();
let stretch_arena = Arena::new();
let stretch_depth = max_depth + 1;
let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth);
......@@ -65,7 +65,7 @@ fn main() {
stretch_depth,
item_check(stretch_tree));
let long_lived_arena = arena::Arena();
let long_lived_arena = Arena::new();
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
let mut depth = min_depth;
while depth <= max_depth {
......
......@@ -11,10 +11,10 @@
// except according to those terms.
extern mod extra;
use extra::arena;
use extra::arena::Arena;
pub fn main() {
let mut arena = arena::Arena();
let mut arena = Arena::new();
let p = &mut arena;
let x = p.alloc(|| 4u);
printf!("%u", *x);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册