Skip to content

Commit 82ec343

Browse files
author
Jose Narvaez
committed
Rustfmt-ing libarena.
1 parent ec4362d commit 82ec343

File tree

1 file changed

+62
-77
lines changed

1 file changed

+62
-77
lines changed

src/libarena/lib.rs

Lines changed: 62 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ pub struct Arena<'longer_than_self> {
105105
head: RefCell<Chunk>,
106106
copy_head: RefCell<Chunk>,
107107
chunks: RefCell<Vec<Chunk>>,
108-
_marker: marker::PhantomData<*mut &'longer_than_self()>,
108+
_marker: marker::PhantomData<*mut &'longer_than_self ()>,
109109
}
110110

111111
impl<'a> Arena<'a> {
@@ -197,10 +197,11 @@ fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
197197
struct TyDesc {
198198
drop_glue: fn(*const i8),
199199
size: usize,
200-
align: usize
200+
align: usize,
201201
}
202202

203-
trait AllTypes { fn dummy(&self) { } }
203+
trait AllTypes { fn dummy(&self) {
204+
} }
204205
impl<T:?Sized> AllTypes for T { }
205206

206207
unsafe fn get_tydesc<T>() -> *const TyDesc {
@@ -224,8 +225,7 @@ impl<'longer_than_self> Arena<'longer_than_self> {
224225
let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
225226
self.chunks.borrow_mut().push(self.copy_head.borrow().clone());
226227

227-
*self.copy_head.borrow_mut() =
228-
chunk((new_min_chunk_size + 1).next_power_of_two(), true);
228+
*self.copy_head.borrow_mut() = chunk((new_min_chunk_size + 1).next_power_of_two(), true);
229229

230230
self.alloc_copy_inner(n_bytes, align)
231231
}
@@ -242,38 +242,34 @@ impl<'longer_than_self> Arena<'longer_than_self> {
242242
let copy_head = self.copy_head.borrow();
243243
copy_head.fill.set(end);
244244

245-
unsafe {
246-
copy_head.as_ptr().offset(start as isize)
247-
}
245+
unsafe { copy_head.as_ptr().offset(start as isize) }
248246
}
249247

250248
#[inline]
251-
fn alloc_copy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
249+
fn alloc_copy<T, F>(&self, op: F) -> &mut T
250+
where F: FnOnce() -> T
251+
{
252252
unsafe {
253-
let ptr = self.alloc_copy_inner(mem::size_of::<T>(),
254-
mem::align_of::<T>());
253+
let ptr = self.alloc_copy_inner(mem::size_of::<T>(), mem::align_of::<T>());
255254
let ptr = ptr as *mut T;
256255
ptr::write(&mut (*ptr), op());
257256
&mut *ptr
258257
}
259258
}
260259

261260
// Functions for the non-POD part of the arena
262-
fn alloc_noncopy_grow(&self, n_bytes: usize,
263-
align: usize) -> (*const u8, *const u8) {
261+
fn alloc_noncopy_grow(&self, n_bytes: usize, align: usize) -> (*const u8, *const u8) {
264262
// Allocate a new chunk.
265263
let new_min_chunk_size = cmp::max(n_bytes, self.chunk_size());
266264
self.chunks.borrow_mut().push(self.head.borrow().clone());
267265

268-
*self.head.borrow_mut() =
269-
chunk((new_min_chunk_size + 1).next_power_of_two(), false);
266+
*self.head.borrow_mut() = chunk((new_min_chunk_size + 1).next_power_of_two(), false);
270267

271268
self.alloc_noncopy_inner(n_bytes, align)
272269
}
273270

274271
#[inline]
275-
fn alloc_noncopy_inner(&self, n_bytes: usize,
276-
align: usize) -> (*const u8, *const u8) {
272+
fn alloc_noncopy_inner(&self, n_bytes: usize, align: usize) -> (*const u8, *const u8) {
277273
// Be careful to not maintain any `head` borrows active, because
278274
// `alloc_noncopy_grow` borrows it mutably.
279275
let (start, end, tydesc_start, head_capacity) = {
@@ -297,24 +293,25 @@ impl<'longer_than_self> Arena<'longer_than_self> {
297293

298294
unsafe {
299295
let buf = head.as_ptr();
300-
(buf.offset(tydesc_start as isize), buf.offset(start as isize))
296+
(buf.offset(tydesc_start as isize),
297+
buf.offset(start as isize))
301298
}
302299
}
303300

304301
#[inline]
305-
fn alloc_noncopy<T, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
302+
fn alloc_noncopy<T, F>(&self, op: F) -> &mut T
303+
where F: FnOnce() -> T
304+
{
306305
unsafe {
307306
let tydesc = get_tydesc::<T>();
308-
let (ty_ptr, ptr) =
309-
self.alloc_noncopy_inner(mem::size_of::<T>(),
310-
mem::align_of::<T>());
307+
let (ty_ptr, ptr) = self.alloc_noncopy_inner(mem::size_of::<T>(), mem::align_of::<T>());
311308
let ty_ptr = ty_ptr as *mut usize;
312309
let ptr = ptr as *mut T;
313310
// Write in our tydesc along with a bit indicating that it
314311
// has *not* been initialized yet.
315312
*ty_ptr = bitpack_tydesc_ptr(tydesc, false);
316313
// Actually initialize it
317-
ptr::write(&mut(*ptr), op());
314+
ptr::write(&mut (*ptr), op());
318315
// Now that we are done, update the tydesc to indicate that
319316
// the object is there.
320317
*ty_ptr = bitpack_tydesc_ptr(tydesc, true);
@@ -326,7 +323,9 @@ impl<'longer_than_self> Arena<'longer_than_self> {
326323
/// Allocates a new item in the arena, using `op` to initialize the value,
327324
/// and returns a reference to it.
328325
#[inline]
329-
pub fn alloc<T:'longer_than_self, F>(&self, op: F) -> &mut T where F: FnOnce() -> T {
326+
pub fn alloc<T: 'longer_than_self, F>(&self, op: F) -> &mut T
327+
where F: FnOnce() -> T
328+
{
330329
unsafe {
331330
if intrinsics::needs_drop::<T>() {
332331
self.alloc_noncopy(op)
@@ -358,10 +357,10 @@ fn test_arena_destructors_fail() {
358357
for i in 0..10 {
359358
// Arena allocate something with drop glue to make sure it
360359
// doesn't leak.
361-
arena.alloc(|| { Rc::new(i) });
360+
arena.alloc(|| Rc::new(i));
362361
// Allocate something with funny size and alignment, to keep
363362
// things interesting.
364-
arena.alloc(|| { [0u8, 1, 2] });
363+
arena.alloc(|| [0u8, 1, 2]);
365364
}
366365
// Now, panic while allocating
367366
arena.alloc::<Rc<i32>, _>(|| {
@@ -393,9 +392,7 @@ struct TypedArenaChunk<T> {
393392
next: *mut TypedArenaChunk<T>,
394393

395394
/// The number of elements that this chunk can hold.
396-
capacity: usize,
397-
398-
// Objects follow here, suitably aligned.
395+
capacity: usize, // Objects follow here, suitably aligned.
399396
}
400397

401398
fn calculate_size<T>(capacity: usize) -> usize {
@@ -409,12 +406,13 @@ fn calculate_size<T>(capacity: usize) -> usize {
409406

410407
impl<T> TypedArenaChunk<T> {
411408
#[inline]
412-
unsafe fn new(next: *mut TypedArenaChunk<T>, capacity: usize)
413-
-> *mut TypedArenaChunk<T> {
409+
unsafe fn new(next: *mut TypedArenaChunk<T>, capacity: usize) -> *mut TypedArenaChunk<T> {
414410
let size = calculate_size::<T>(capacity);
415-
let chunk = allocate(size, mem::align_of::<TypedArenaChunk<T>>())
416-
as *mut TypedArenaChunk<T>;
417-
if chunk.is_null() { alloc::oom() }
411+
let chunk =
412+
allocate(size, mem::align_of::<TypedArenaChunk<T>>()) as *mut TypedArenaChunk<T>;
413+
if chunk.is_null() {
414+
alloc::oom()
415+
}
418416
(*chunk).next = next;
419417
(*chunk).capacity = capacity;
420418
chunk
@@ -437,7 +435,8 @@ impl<T> TypedArenaChunk<T> {
437435
let next = self.next;
438436
let size = calculate_size::<T>(self.capacity);
439437
let self_ptr: *mut TypedArenaChunk<T> = self;
440-
deallocate(self_ptr as *mut u8, size,
438+
deallocate(self_ptr as *mut u8,
439+
size,
441440
mem::align_of::<TypedArenaChunk<T>>());
442441
if !next.is_null() {
443442
let capacity = (*next).capacity;
@@ -449,9 +448,7 @@ impl<T> TypedArenaChunk<T> {
449448
#[inline]
450449
fn start(&self) -> *const u8 {
451450
let this: *const TypedArenaChunk<T> = self;
452-
unsafe {
453-
round_up(this.offset(1) as usize, mem::align_of::<T>()) as *const u8
454-
}
451+
unsafe { round_up(this.offset(1) as usize, mem::align_of::<T>()) as *const u8 }
455452
}
456453

457454
// Returns a pointer to the end of the allocated space.
@@ -545,22 +542,29 @@ mod tests {
545542

546543
#[test]
547544
fn test_arena_alloc_nested() {
548-
struct Inner { value: u8 }
549-
struct Outer<'a> { inner: &'a Inner }
550-
enum EI<'e> { I(Inner), O(Outer<'e>) }
545+
struct Inner {
546+
value: u8,
547+
}
548+
struct Outer<'a> {
549+
inner: &'a Inner,
550+
}
551+
enum EI<'e> {
552+
I(Inner),
553+
O(Outer<'e>),
554+
}
551555

552556
struct Wrap<'a>(TypedArena<EI<'a>>);
553557

554558
impl<'a> Wrap<'a> {
555-
fn alloc_inner<F:Fn() -> Inner>(&self, f: F) -> &Inner {
559+
fn alloc_inner<F: Fn() -> Inner>(&self, f: F) -> &Inner {
556560
let r: &EI = self.0.alloc(EI::I(f()));
557561
if let &EI::I(ref i) = r {
558562
i
559563
} else {
560564
panic!("mismatch");
561565
}
562566
}
563-
fn alloc_outer<F:Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
567+
fn alloc_outer<F: Fn() -> Outer<'a>>(&self, f: F) -> &Outer {
564568
let r: &EI = self.0.alloc(EI::O(f()));
565569
if let &EI::O(ref o) = r {
566570
o
@@ -572,8 +576,9 @@ mod tests {
572576

573577
let arena = Wrap(TypedArena::new());
574578

575-
let result = arena.alloc_outer(|| Outer {
576-
inner: arena.alloc_inner(|| Inner { value: 10 }) });
579+
let result = arena.alloc_outer(|| {
580+
Outer { inner: arena.alloc_inner(|| Inner { value: 10 }) }
581+
});
577582

578583
assert_eq!(result.inner.value, 10);
579584
}
@@ -582,49 +587,27 @@ mod tests {
582587
pub fn test_copy() {
583588
let arena = TypedArena::new();
584589
for _ in 0..100000 {
585-
arena.alloc(Point {
586-
x: 1,
587-
y: 2,
588-
z: 3,
589-
});
590+
arena.alloc(Point { x: 1, y: 2, z: 3 });
590591
}
591592
}
592593

593594
#[bench]
594595
pub fn bench_copy(b: &mut Bencher) {
595596
let arena = TypedArena::new();
596-
b.iter(|| {
597-
arena.alloc(Point {
598-
x: 1,
599-
y: 2,
600-
z: 3,
601-
})
602-
})
597+
b.iter(|| arena.alloc(Point { x: 1, y: 2, z: 3 }))
603598
}
604599

605600
#[bench]
606601
pub fn bench_copy_nonarena(b: &mut Bencher) {
607602
b.iter(|| {
608-
let _: Box<_> = box Point {
609-
x: 1,
610-
y: 2,
611-
z: 3,
612-
};
603+
let _: Box<_> = box Point { x: 1, y: 2, z: 3 };
613604
})
614605
}
615606

616607
#[bench]
617608
pub fn bench_copy_old_arena(b: &mut Bencher) {
618609
let arena = Arena::new();
619-
b.iter(|| {
620-
arena.alloc(|| {
621-
Point {
622-
x: 1,
623-
y: 2,
624-
z: 3,
625-
}
626-
})
627-
})
610+
b.iter(|| arena.alloc(|| Point { x: 1, y: 2, z: 3 }))
628611
}
629612

630613
#[allow(dead_code)]
@@ -639,7 +622,7 @@ mod tests {
639622
for _ in 0..100000 {
640623
arena.alloc(Noncopy {
641624
string: "hello world".to_string(),
642-
array: vec!( 1, 2, 3, 4, 5 ),
625+
array: vec!(1, 2, 3, 4, 5),
643626
});
644627
}
645628
}
@@ -650,7 +633,7 @@ mod tests {
650633
b.iter(|| {
651634
arena.alloc(Noncopy {
652635
string: "hello world".to_string(),
653-
array: vec!( 1, 2, 3, 4, 5 ),
636+
array: vec!(1, 2, 3, 4, 5),
654637
})
655638
})
656639
}
@@ -660,7 +643,7 @@ mod tests {
660643
b.iter(|| {
661644
let _: Box<_> = box Noncopy {
662645
string: "hello world".to_string(),
663-
array: vec!( 1, 2, 3, 4, 5 ),
646+
array: vec!(1, 2, 3, 4, 5),
664647
};
665648
})
666649
}
@@ -669,9 +652,11 @@ mod tests {
669652
pub fn bench_noncopy_old_arena(b: &mut Bencher) {
670653
let arena = Arena::new();
671654
b.iter(|| {
672-
arena.alloc(|| Noncopy {
673-
string: "hello world".to_string(),
674-
array: vec!( 1, 2, 3, 4, 5 ),
655+
arena.alloc(|| {
656+
Noncopy {
657+
string: "hello world".to_string(),
658+
array: vec!(1, 2, 3, 4, 5),
659+
}
675660
})
676661
})
677662
}

0 commit comments

Comments
 (0)