Skip to content

Commit 8ad0344

Browse files
committed
Cargo fmt
1 parent ef4fd41 commit 8ad0344

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+319
-283
lines changed

aiscript-arena/src/allocator_api.rs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ unsafe impl<A: Allocator> Allocator for MetricsAlloc<'_, A> {
6868
}
6969

7070
#[inline]
71-
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { unsafe {
72-
self.metrics.mark_external_deallocation(layout.size());
73-
self.allocator.deallocate(ptr, layout);
74-
}}
71+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
72+
unsafe {
73+
self.metrics.mark_external_deallocation(layout.size());
74+
self.allocator.deallocate(ptr, layout);
75+
}
76+
}
7577

7678
#[inline]
7779
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
@@ -86,38 +88,44 @@ unsafe impl<A: Allocator> Allocator for MetricsAlloc<'_, A> {
8688
ptr: NonNull<u8>,
8789
old_layout: Layout,
8890
new_layout: Layout,
89-
) -> Result<NonNull<[u8]>, AllocError> { unsafe {
90-
let ptr = self.allocator.grow(ptr, old_layout, new_layout)?;
91-
self.metrics
92-
.mark_external_allocation(new_layout.size() - old_layout.size());
93-
Ok(ptr)
94-
}}
91+
) -> Result<NonNull<[u8]>, AllocError> {
92+
unsafe {
93+
let ptr = self.allocator.grow(ptr, old_layout, new_layout)?;
94+
self.metrics
95+
.mark_external_allocation(new_layout.size() - old_layout.size());
96+
Ok(ptr)
97+
}
98+
}
9599

96100
#[inline]
97101
unsafe fn grow_zeroed(
98102
&self,
99103
ptr: NonNull<u8>,
100104
old_layout: Layout,
101105
new_layout: Layout,
102-
) -> Result<NonNull<[u8]>, AllocError> { unsafe {
103-
let ptr = self.allocator.grow_zeroed(ptr, old_layout, new_layout)?;
104-
self.metrics
105-
.mark_external_allocation(new_layout.size() - old_layout.size());
106-
Ok(ptr)
107-
}}
106+
) -> Result<NonNull<[u8]>, AllocError> {
107+
unsafe {
108+
let ptr = self.allocator.grow_zeroed(ptr, old_layout, new_layout)?;
109+
self.metrics
110+
.mark_external_allocation(new_layout.size() - old_layout.size());
111+
Ok(ptr)
112+
}
113+
}
108114

109115
#[inline]
110116
unsafe fn shrink(
111117
&self,
112118
ptr: NonNull<u8>,
113119
old_layout: Layout,
114120
new_layout: Layout,
115-
) -> Result<NonNull<[u8]>, AllocError> { unsafe {
116-
let ptr = self.allocator.shrink(ptr, old_layout, new_layout)?;
117-
self.metrics
118-
.mark_external_deallocation(old_layout.size() - new_layout.size());
119-
Ok(ptr)
120-
}}
121+
) -> Result<NonNull<[u8]>, AllocError> {
122+
unsafe {
123+
let ptr = self.allocator.shrink(ptr, old_layout, new_layout)?;
124+
self.metrics
125+
.mark_external_deallocation(old_layout.size() - new_layout.size());
126+
Ok(ptr)
127+
}
128+
}
121129
}
122130

123131
unsafe impl<A: 'static> Collect for MetricsAlloc<'_, A> {

aiscript-arena/src/arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use alloc::boxed::Box;
22
use core::{f64, marker::PhantomData};
33

44
use crate::{
5+
Collect,
56
context::{Context, EarlyStop, Finalization, Mutation, Phase},
67
metrics::Metrics,
7-
Collect,
88
};
99

1010
/// A trait that produces a [`Collect`]-able type for the given lifetime. This is used to produce

aiscript-arena/src/barrier.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ impl<T: ?Sized> Write<T> {
4545
/// by this wrapper, unless [`Gc::write`] is invoked manually on the parent [`Gc`]
4646
/// pointer during the current arena callback.
4747
#[inline(always)]
48-
pub unsafe fn assume(v: &T) -> &Self { unsafe {
49-
// SAFETY: `Self` is `repr(transparent)`.
50-
mem::transmute(v)
51-
}}
48+
pub unsafe fn assume(v: &T) -> &Self {
49+
unsafe {
50+
// SAFETY: `Self` is `repr(transparent)`.
51+
mem::transmute(v)
52+
}
53+
}
5254

5355
/// Gets a writable reference to non-GC'd data.
5456
///
@@ -74,10 +76,12 @@ impl<T: ?Sized> Write<T> {
7476
/// Implementation detail of `write_field!`; same safety requirements as `assume`.
7577
#[inline(always)]
7678
#[doc(hidden)]
77-
pub unsafe fn __from_ref_and_ptr(v: &T, _: *const T) -> &Self { unsafe {
78-
// SAFETY: `Self` is `repr(transparent)`.
79-
mem::transmute(v)
80-
}}
79+
pub unsafe fn __from_ref_and_ptr(v: &T, _: *const T) -> &Self {
80+
unsafe {
81+
// SAFETY: `Self` is `repr(transparent)`.
82+
mem::transmute(v)
83+
}
84+
}
8185

8286
/// Unlocks the referenced value, providing full interior mutability.
8387
#[inline]

aiscript-arena/src/context.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use core::{
77
};
88

99
use crate::{
10+
Gc,
1011
collect::Collect,
1112
metrics::Metrics,
1213
types::{GcBox, GcBoxHeader, GcBoxInner, GcColor, Invariant},
13-
Gc,
1414
};
1515

1616
/// Handle value given by arena callbacks during construction and mutation. Allows allocating new
@@ -216,9 +216,9 @@ impl Context {
216216
}
217217

218218
#[inline]
219-
pub(crate) unsafe fn mutation_context<'gc>(&self) -> &Mutation<'gc> { unsafe {
220-
mem::transmute::<&Self, &Mutation>(self)
221-
}}
219+
pub(crate) unsafe fn mutation_context<'gc>(&self) -> &Mutation<'gc> {
220+
unsafe { mem::transmute::<&Self, &Mutation>(self) }
221+
}
222222

223223
#[inline]
224224
fn collection_context(&self) -> &Collection {
@@ -227,9 +227,9 @@ impl Context {
227227
}
228228

229229
#[inline]
230-
pub(crate) unsafe fn finalization_context<'gc>(&self) -> &Finalization<'gc> { unsafe {
231-
mem::transmute::<&Self, &Finalization>(self)
232-
}}
230+
pub(crate) unsafe fn finalization_context<'gc>(&self) -> &Finalization<'gc> {
231+
unsafe { mem::transmute::<&Self, &Finalization>(self) }
232+
}
233233

234234
#[inline]
235235
pub(crate) fn metrics(&self) -> &Metrics {
@@ -302,7 +302,9 @@ impl Context {
302302
let next_gray = if let Some(gc_box) = self.gray.borrow_mut().pop() {
303303
self.metrics.mark_gc_traced(gc_box.header().size_of_box());
304304
Some(gc_box)
305-
} else { self.gray_again.borrow_mut().pop() };
305+
} else {
306+
self.gray_again.borrow_mut().pop()
307+
};
306308

307309
if let Some(gc_box) = next_gray {
308310
// If we have an object in the gray queue, take one, trace it, and turn it
@@ -586,13 +588,15 @@ impl Context {
586588
}
587589

588590
// SAFETY: the gc_box must never be accessed after calling this function.
589-
unsafe fn free_gc_box<'gc>(mut gc_box: GcBox) { unsafe {
590-
if gc_box.header().is_live() {
591-
// If the alive flag is set, that means we haven't dropped the inner value of this object,
592-
gc_box.drop_in_place();
591+
unsafe fn free_gc_box<'gc>(mut gc_box: GcBox) {
592+
unsafe {
593+
if gc_box.header().is_live() {
594+
// If the alive flag is set, that means we haven't dropped the inner value of this object,
595+
gc_box.drop_in_place();
596+
}
597+
gc_box.dealloc();
593598
}
594-
gc_box.dealloc();
595-
}}
599+
}
596600

597601
/// Helper type for managing phase transitions.
598602
struct PhaseGuard<'a> {

aiscript-arena/src/dynamic_roots.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use alloc::{
55
vec::Vec,
66
};
77

8-
use crate::{arena::Root, metrics::Metrics, Collect, Gc, Mutation, Rootable};
8+
use crate::{Collect, Gc, Mutation, Rootable, arena::Root, metrics::Metrics};
99

1010
/// A way of registering GC roots dynamically.
1111
///

aiscript-arena/src/gc.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use core::{
99
};
1010

1111
use crate::{
12+
Finalization,
1213
barrier::{Unlock, Write},
1314
collect::Collect,
1415
context::{Collection, Mutation},
1516
gc_weak::GcWeak,
1617
static_collect::Static,
1718
types::{GcBox, GcBoxHeader, GcBoxInner, GcColor, Invariant},
18-
Finalization,
1919
};
2020

2121
/// A garbage collected pointer to a type T. Implements Copy, and is implemented as a plain machine
@@ -152,16 +152,18 @@ impl<'gc, T: ?Sized + 'gc> Gc<'gc, T> {
152152
/// The provided pointer must have been obtained from `Gc::as_ptr`, and the pointer must not
153153
/// have been collected yet.
154154
#[inline]
155-
pub unsafe fn from_ptr(ptr: *const T) -> Gc<'gc, T> { unsafe {
156-
let layout = Layout::new::<GcBoxHeader>();
157-
let (_, header_offset) = layout.extend(Layout::for_value(&*ptr)).unwrap();
158-
let header_offset = -(header_offset as isize);
159-
let ptr = (ptr as *mut T).byte_offset(header_offset) as *mut GcBoxInner<T>;
160-
Gc {
161-
ptr: NonNull::new_unchecked(ptr),
162-
_invariant: PhantomData,
155+
pub unsafe fn from_ptr(ptr: *const T) -> Gc<'gc, T> {
156+
unsafe {
157+
let layout = Layout::new::<GcBoxHeader>();
158+
let (_, header_offset) = layout.extend(Layout::for_value(&*ptr)).unwrap();
159+
let header_offset = -(header_offset as isize);
160+
let ptr = (ptr as *mut T).byte_offset(header_offset) as *mut GcBoxInner<T>;
161+
Gc {
162+
ptr: NonNull::new_unchecked(ptr),
163+
_invariant: PhantomData,
164+
}
163165
}
164-
}}
166+
}
165167
}
166168

167169
impl<'gc, T: Unlock + ?Sized + 'gc> Gc<'gc, T> {

aiscript-arena/src/gc_weak.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,13 @@ impl<'gc, T: 'gc> GcWeak<'gc, T> {
112112
/// SAFETY:
113113
/// It must be valid to dereference a `*mut U` that has come from casting a `*mut T`.
114114
#[inline]
115-
pub unsafe fn cast<U: 'gc>(this: GcWeak<'gc, T>) -> GcWeak<'gc, U> { unsafe {
116-
GcWeak {
117-
inner: Gc::cast::<U>(this.inner),
115+
pub unsafe fn cast<U: 'gc>(this: GcWeak<'gc, T>) -> GcWeak<'gc, U> {
116+
unsafe {
117+
GcWeak {
118+
inner: Gc::cast::<U>(this.inner),
119+
}
118120
}
119-
}}
121+
}
120122

121123
/// Retrieve a `GcWeak` from a raw pointer obtained from `GcWeak::as_ptr`
122124
///
@@ -125,9 +127,11 @@ impl<'gc, T: 'gc> GcWeak<'gc, T> {
125127
/// the pointer must not have been *fully* collected yet (it may be a dropped but valid weak
126128
/// pointer).
127129
#[inline]
128-
pub unsafe fn from_ptr(ptr: *const T) -> GcWeak<'gc, T> { unsafe {
129-
GcWeak {
130-
inner: Gc::from_ptr(ptr),
130+
pub unsafe fn from_ptr(ptr: *const T) -> GcWeak<'gc, T> {
131+
unsafe {
132+
GcWeak {
133+
inner: Gc::from_ptr(ptr),
134+
}
131135
}
132-
}}
136+
}
133137
}

aiscript-arena/src/lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::{
66
fmt,
77
};
88

9-
use crate::{barrier::Unlock, Collect, Collection, Gc, Mutation};
9+
use crate::{Collect, Collection, Gc, Mutation, barrier::Unlock};
1010

1111
// Helper macro to factor out the common parts of locks types.
1212
macro_rules! make_lock_wrapper {

aiscript-arena/src/static_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::collect::Collect;
21
use crate::Rootable;
2+
use crate::collect::Collect;
33

44
use alloc::borrow::{Borrow, BorrowMut};
55
use core::convert::{AsMut, AsRef};

aiscript-arena/src/types.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ impl GcBox {
1919
/// **SAFETY:** The pointer must point to a valid `GcBoxInner` allocated
2020
/// in a `Box`.
2121
#[inline(always)]
22-
pub(crate) unsafe fn erase<T: ?Sized>(ptr: NonNull<GcBoxInner<T>>) -> Self { unsafe {
23-
// This cast is sound because `GcBoxInner` is `repr(C)`.
24-
let erased = ptr.as_ptr() as *mut GcBoxInner<()>;
25-
Self(NonNull::new_unchecked(erased))
26-
}}
22+
pub(crate) unsafe fn erase<T: ?Sized>(ptr: NonNull<GcBoxInner<T>>) -> Self {
23+
unsafe {
24+
// This cast is sound because `GcBoxInner` is `repr(C)`.
25+
let erased = ptr.as_ptr() as *mut GcBoxInner<()>;
26+
Self(NonNull::new_unchecked(erased))
27+
}
28+
}
2729

2830
/// Gets a pointer to the value stored inside this box.
2931
/// `T` must be the same type that was used with `erase`, so that
@@ -47,31 +49,33 @@ impl GcBox {
4749
///
4850
/// **SAFETY**: `Self::drop_in_place` must not have been called.
4951
#[inline(always)]
50-
pub(crate) unsafe fn trace_value(&self, cc: &crate::Collection) { unsafe {
51-
(self.header().vtable().trace_value)(*self, cc)
52-
}}
52+
pub(crate) unsafe fn trace_value(&self, cc: &crate::Collection) {
53+
unsafe { (self.header().vtable().trace_value)(*self, cc) }
54+
}
5355

5456
/// Drops the stored value.
5557
///
5658
/// **SAFETY**: once called, no GC pointers should access the stored value
5759
/// (but accessing the `GcBox` itself is still safe).
5860
#[inline(always)]
59-
pub(crate) unsafe fn drop_in_place(&mut self) { unsafe {
60-
(self.header().vtable().drop_value)(*self)
61-
}}
61+
pub(crate) unsafe fn drop_in_place(&mut self) {
62+
unsafe { (self.header().vtable().drop_value)(*self) }
63+
}
6264

6365
/// Deallocates the box. Failing to call `Self::drop_in_place` beforehand
6466
/// will cause the stored value to be leaked.
6567
///
6668
/// **SAFETY**: once called, this `GcBox` should never be accessed by any GC
6769
/// pointers again.
6870
#[inline(always)]
69-
pub(crate) unsafe fn dealloc(self) { unsafe {
70-
let layout = self.header().vtable().box_layout;
71-
let ptr = self.0.as_ptr() as *mut u8;
72-
// SAFETY: the pointer was `Box`-allocated with this layout.
73-
alloc::alloc::dealloc(ptr, layout);
74-
}}
71+
pub(crate) unsafe fn dealloc(self) {
72+
unsafe {
73+
let layout = self.header().vtable().box_layout;
74+
let ptr = self.0.as_ptr() as *mut u8;
75+
// SAFETY: the pointer was `Box`-allocated with this layout.
76+
alloc::alloc::dealloc(ptr, layout);
77+
}
78+
}
7579
}
7680

7781
pub(crate) struct GcBoxHeader {

0 commit comments

Comments
 (0)