Skip to content

Commit c2bacd2

Browse files
committed
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)?
2 parents dbaca98 + eab97b5 commit c2bacd2

File tree

3 files changed

+30
-28
lines changed

3 files changed

+30
-28
lines changed

src/libextra/arena.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ pub struct Arena {
6767
priv chunks: @mut MutList<Chunk>,
6868
}
6969

70-
#[unsafe_destructor]
71-
impl Drop for Arena {
72-
fn drop(&self) {
73-
unsafe {
74-
destroy_chunk(&self.head);
75-
do self.chunks.each |chunk| {
76-
if !chunk.is_pod {
77-
destroy_chunk(chunk);
78-
}
79-
true
80-
};
70+
impl Arena {
71+
pub fn new() -> Arena {
72+
Arena::new_with_size(32u)
73+
}
74+
75+
pub fn new_with_size(initial_size: uint) -> Arena {
76+
Arena {
77+
head: chunk(initial_size, false),
78+
pod_head: chunk(initial_size, true),
79+
chunks: @mut MutNil,
8180
}
8281
}
8382
}
@@ -92,18 +91,21 @@ fn chunk(size: uint, is_pod: bool) -> Chunk {
9291
}
9392
}
9493

95-
pub fn arena_with_size(initial_size: uint) -> Arena {
96-
Arena {
97-
head: chunk(initial_size, false),
98-
pod_head: chunk(initial_size, true),
99-
chunks: @mut MutNil,
94+
#[unsafe_destructor]
95+
impl Drop for Arena {
96+
fn drop(&self) {
97+
unsafe {
98+
destroy_chunk(&self.head);
99+
do self.chunks.each |chunk| {
100+
if !chunk.is_pod {
101+
destroy_chunk(chunk);
102+
}
103+
true
104+
};
105+
}
100106
}
101107
}
102108

103-
pub fn Arena() -> Arena {
104-
arena_with_size(32u)
105-
}
106-
107109
#[inline]
108110
fn round_up_to(base: uint, align: uint) -> uint {
109111
(base + (align - 1)) & !(align - 1)
@@ -276,7 +278,7 @@ impl Arena {
276278

277279
#[test]
278280
fn test_arena_destructors() {
279-
let arena = Arena();
281+
let arena = Arena::new();
280282
for i in range(0u, 10) {
281283
// Arena allocate something with drop glue to make sure it
282284
// doesn't leak.
@@ -291,7 +293,7 @@ fn test_arena_destructors() {
291293
#[should_fail]
292294
#[ignore(cfg(windows))]
293295
fn test_arena_destructors_fail() {
294-
let arena = Arena();
296+
let arena = Arena::new();
295297
// Put some stuff in the arena.
296298
for i in range(0u, 10) {
297299
// Arena allocate something with drop glue to make sure it

src/test/bench/shootout-binarytrees.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
extern mod extra;
12-
use extra::arena;
12+
use extra::arena::Arena;
1313

1414
enum Tree<'self> {
1515
Nil,
@@ -25,7 +25,7 @@ fn item_check(t: &Tree) -> int {
2525
}
2626
}
2727

28-
fn bottom_up_tree<'r>(arena: &'r arena::Arena, item: int, depth: int)
28+
fn bottom_up_tree<'r>(arena: &'r Arena, item: int, depth: int)
2929
-> &'r Tree<'r> {
3030
if depth > 0 {
3131
return arena.alloc(
@@ -57,15 +57,15 @@ fn main() {
5757
max_depth = n;
5858
}
5959

60-
let stretch_arena = arena::Arena();
60+
let stretch_arena = Arena::new();
6161
let stretch_depth = max_depth + 1;
6262
let stretch_tree = bottom_up_tree(&stretch_arena, 0, stretch_depth);
6363

6464
printfln!("stretch tree of depth %d\t check: %d",
6565
stretch_depth,
6666
item_check(stretch_tree));
6767

68-
let long_lived_arena = arena::Arena();
68+
let long_lived_arena = Arena::new();
6969
let long_lived_tree = bottom_up_tree(&long_lived_arena, 0, max_depth);
7070
let mut depth = min_depth;
7171
while depth <= max_depth {

src/test/run-pass/placement-new-arena.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
// except according to those terms.
1212

1313
extern mod extra;
14-
use extra::arena;
14+
use extra::arena::Arena;
1515

1616
pub fn main() {
17-
let mut arena = arena::Arena();
17+
let mut arena = Arena::new();
1818
let p = &mut arena;
1919
let x = p.alloc(|| 4u);
2020
printf!("%u", *x);

0 commit comments

Comments
 (0)