gruggers 0.9.0

rust implementation of the grug language
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use std::ptr::NonNull;
use std::marker::PhantomData;
use std::mem::{size_of, ManuallyDrop};
use std::alloc::{alloc, Layout, handle_alloc_error, dealloc};
use std::cell::Cell;

mod typed_xar {
	use super::*;

	/// A growable exponential array 
	/// Insertion returns a pinned pointer to a value.
	/// The values inside the Xar are not dropped unless delete is called with a pointer to the item
	/// This also includes the destructor. 
	/// The destructor simply deallocates the memory and does not drop the values in it.
	pub struct Xar<T> {
		committed: Cell<usize>,
		inner: NonNull<XarInner<T>>,
	}
	// # SAFETY
	// Sending ownership to a different thread requiries that there are no 
	// current references into the Xar
	unsafe impl<T: Send> Send for Xar<T> {}

	#[repr(C)]
	union XarStorage<T> {
		value: ManuallyDrop<T>,
		free_list: Option<NonNull<Self>>,
	}

	impl<T> XarStorage<T> {
		fn value(value: T) -> Self {
			Self {
				value: ManuallyDrop::new(value),
			}
		}

		fn free_list(free_list: Option<NonNull<Self>>) -> Self {
			Self {
				free_list
			}
		}
	}

	struct XarInner<T> {
		free_list: Cell<Option<NonNull<XarStorage<T>>>>,
		chunks: [Cell<Option<NonNull<XarStorage<T>>>>; 62],
		// TODO:
		// allocator: A
	}

	impl<T> Xar<T> {
		const _ASSERT_1: () = assert!(size_of::<XarHandle<'static, T>>() == size_of::<XarHandle<'static, Option<T>>>());
		pub const FIRST_SIZE: usize = const {
			let size = size_of::<XarStorage<T>>();
			if 64 / size > 8 {64 / size} else {8}
		};
		pub fn new() -> Self {
			Self {
				committed: Cell::new(0),
				inner: Self::create_inner(),
			}
		}

		pub fn insert(&self, value: T) -> XarHandle<'_, T> {
			if size_of::<T>() == 0 {
				return XarHandle::new(NonNull::dangling());
			}
			let inner = unsafe{&*self.inner.as_ptr()};
			if let Some(free) = inner.free_list.get() {
				unsafe{
					let next = (*free.as_ptr()).free_list;
					*free.as_ptr() = XarStorage::value(value);
					inner.free_list.set(next);
					XarHandle::new(free)
				}
			} else {
				let location = Self::calc_location(self.committed.get());
				let chunk = unsafe{&(*self.inner.as_ptr()).chunks[location.0]};
				let chunk = if let Some(chunk) = chunk.get() {
					chunk
				} else {
					let ret_val = Self::alloc_chunk(location.0);
					chunk.set(Some(ret_val));
					ret_val
				};
				unsafe {
					chunk.add(location.1).write(XarStorage::value(value));
				}
				self.committed.update(|x| x + 1);
				unsafe {
					XarHandle::new(chunk.add(location.1))
				}
			}
		}

		fn alloc_chunk(bucket_idx: usize) -> NonNull<XarStorage<T>> {
			let chunk_size = Self::chunk_size(bucket_idx);

			let layout = Layout::array::<XarStorage<T>>(chunk_size)
				.expect("layout invalid");
			let ptr = unsafe{alloc(layout)}.cast::<XarStorage<T>>();
			if ptr.is_null() {
				handle_alloc_error(layout);
			}
			unsafe{NonNull::new_unchecked(ptr)}
		}

		fn chunk_size(bucket_idx: usize) -> usize {
			if bucket_idx == 0 || bucket_idx == 1 {
				Self::FIRST_SIZE
			} else {
				let mut bucket_size = Self::FIRST_SIZE * 2;
				(2..bucket_idx).for_each(|_| bucket_size *= 2);
				bucket_size
			}
		}

		/// Checks if the handle is actually from within self
		pub fn contains(&self, handle: XarHandle<T>) -> bool {
			if size_of::<T>() == 0 {
				return true;
			}
			let inner = unsafe{&*self.inner.as_ptr()};
			let mut current_bucket_size = Self::FIRST_SIZE;
			for bucket in &inner.chunks {
				let Some(bucket) = bucket.get() else {return false};
				if (handle.0.as_ptr().addr()).wrapping_sub(bucket.as_ptr().addr()) < current_bucket_size {
					return true;
				}
				current_bucket_size *= 2;
			}
			false
		}

		/// # SAFETY
		/// handle must be from the current Xar
		pub unsafe fn delete(&self, handle: XarHandle<T>) {
			if size_of::<T>() == 0 {
				return;
			}
			unsafe{
				std::ptr::drop_in_place(handle.0.as_ptr());
			}
			let free_list = unsafe{&(*self.inner.as_ptr()).free_list};
			unsafe{*handle.0.as_ptr() = XarStorage::free_list(free_list.get())};
			free_list.set(Some(handle.0));
		}

		fn calc_location(idx: usize) -> (usize, usize) {
			if idx < Self::FIRST_SIZE {
				(0, idx)
			} else if idx < Self::FIRST_SIZE * 2 {
				(1, idx - Self::FIRST_SIZE)
			} else {
				let mut bucket = 2;
				let mut bucket_max = Self::FIRST_SIZE * 4;
				while bucket_max <= idx {
					bucket += 1;
					bucket_max *= 2;
				}
				(bucket, idx - bucket_max / 2)
			}
		}

		fn create_inner() -> NonNull<XarInner<T>> {
			if size_of::<T>() == 0 {
				return NonNull::dangling();
			}
			let layout = Layout::new::<XarInner<T>>();
			let ptr = unsafe{alloc(layout).cast::<XarInner<T>>()};
			if ptr.is_null() {
				handle_alloc_error(layout);
			}
			// SAFETY: NonNull and aligned
			unsafe {
				ptr.write(XarInner{
					free_list: Cell::new(None),
					chunks: std::array::from_fn(|_| Cell::new(None)),
				});
			}
			// SAFETY: NonNull
			unsafe{NonNull::new_unchecked(ptr)}
		}

		pub fn clear(&mut self) {
			if size_of::<T>() != 0 {
				*self.committed.get_mut() = 0;
				unsafe{*(*self.inner.as_ptr()).free_list.get_mut() = None};
			}
		}
	}

	impl<T> Drop for Xar<T> {
		fn drop (&mut self) {
			if size_of::<T>() == 0 {
				return;
			}
			let inner = unsafe{&mut (*self.inner.as_ptr())};
			for (i, chunk) in inner.chunks.iter_mut().filter_map(|x| x.get_mut().as_mut()).enumerate() {
				let chunk_size = Self::chunk_size(i);
				let layout = Layout::array::<XarStorage<T>>(chunk_size).unwrap();
				unsafe{dealloc(chunk.as_ptr().cast::<u8>(), layout)};
			}
			let layout = Layout::new::<XarInner<T>>();
			unsafe{dealloc(self.inner.as_ptr().cast::<u8>(), layout)};
		}
	}
	
	/// An unsafe pointer to a slot in a Xar
	/// 
	/// All access to the underlying data is unsafe.
	/// Users are encouraged to create a newtype around this handle to select
	/// either shared or unique semantics based on what they require.
	///
	/// The value in the slot is leaked if the XarHandle to it is dropped. 
	/// The only way to drop the value is to pass it back to the owning Xar
	/// which will also free the slot
	#[repr(transparent)]
	pub struct XarHandle<'a, T> (NonNull<XarStorage<T>>, PhantomData<&'a ()>);
	impl<'a, T> Clone for XarHandle<'a, T> {
		fn clone(&self) -> Self {
			*self
		}
	}
	impl<'a, T> Copy for XarHandle<'a, T> {}

	impl<'a, T> XarHandle<'a, T> {
		fn new(ptr: NonNull<XarStorage<T>>) -> Self {
			Self(ptr, PhantomData)
		}

		/// SAFETY: The pointer MUST be derived from a XarHandle of the same type
		pub unsafe fn from_ptr(ptr: NonNull<T>) -> Self {
			Self(ptr.cast(), PhantomData)
		}

		pub fn as_ptr(self) -> NonNull<T> {
			self.0.cast()
		}

		pub unsafe fn get_ref(self) -> &'a T {
			unsafe{& (*self.0.as_ptr()).value}
		}

		pub unsafe fn get_mut(self) -> &'a mut T {
			unsafe{&mut (*self.0.as_ptr()).value}
		}

		/// # SAFETY 
		/// Some of the safety guarantees of Xar require the returned handles to be
		/// tied to its own lifetime. This makes storing these handles problematic
		/// sometimes. 
		///
		/// Take care when using the returned handle to ensure that the value is
		/// only used for as long as it is allowed to 
		pub unsafe fn detach_lifetime(self) -> XarHandle<'static, T> {
			unsafe{
				std::mem::transmute::<Self, XarHandle<'static, T>>(self)
			}
		}
	}

	impl<'a, T: std::fmt::Debug> std::fmt::Debug for XarHandle<'a, T> {
		fn fmt (&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
			f.debug_struct("XarHandle")
				.field("ptr", &self.0)
				.finish()
		}
	}

	#[cfg(test)]
	mod test {
		use super::*;
		#[test]
		fn xar_test_1 () {
			assert_eq!(Xar::<usize>::FIRST_SIZE, 8);
			assert_eq!(Xar::<usize>::calc_location(0), (0, 0));
			assert_eq!(Xar::<usize>::calc_location(7), (0, 7));
			assert_eq!(Xar::<usize>::calc_location(8), (1, 0));

			assert_eq!(Xar::<usize>::calc_location(15), (1, 7));
			assert_eq!(Xar::<usize>::calc_location(16), (2, 0));
			assert_eq!(Xar::<usize>::calc_location(31), (2, 15));
			assert_eq!(Xar::<usize>::calc_location(32), (3, 0));
		}

		#[test]
		fn xar_test_2 () {
			let x = Xar::new();
			let x_1 = x.insert(25);
			let x_2 = x.insert(26);
			let x_3 = x.insert(27);
			let x_4 = x.insert(28);
			assert_eq!(25, unsafe{*x_1.get_ref()});
			assert_eq!(26, unsafe{*x_2.get_ref()});
			assert_eq!(27, unsafe{*x_3.get_ref()});
			assert_eq!(28, unsafe{*x_4.get_ref()});

			*unsafe{x_1.get_mut()} += 5;
			*unsafe{x_2.get_mut()} += 5;
			*unsafe{x_3.get_mut()} += 5;
			*unsafe{x_4.get_mut()} += 5;

			assert_eq!(30, unsafe{*x_1.get_ref()});
			assert_eq!(31, unsafe{*x_2.get_ref()});
			assert_eq!(32, unsafe{*x_3.get_ref()});
			assert_eq!(33, unsafe{*x_4.get_ref()});
			eprintln!("{:?}", x_1);
		}

		#[test]
		fn xar_test_3 () {
			let x = Xar::new();
			let mut vec = Vec::new();
			for i in 0..1000 {
				unsafe{vec.push(x.insert(i).get_mut())};
			}
			for i in 0..1000 {
				*vec[i] *= 2;
			}
			for i in 0..1000 {
				assert_eq!(*vec[i], 2 * i);
			}
		}

		// #[test]
		// fn xar_test_4 () {
		// 	let x = Xar::new();
		// 	let mut vec = Vec::new();
		// 	for i in 0..1000 {
		// 		unsafe{vec.push(x.insert(()))};
		// 	}
		// 	for item in vec {
		// 		unsafe{x.delete(item)}
		// 	}
		// }
	}
}
pub use typed_xar::*;

mod erased_xar {
	use super::*;
	/// A growable exponential array where the size of the allocations are determined at runtime.
	/// Insertion returns a pinned pointer to a value.
	/// The values inside the Xar are not dropped unless delete is called with a pointer to the item.
	/// The destructor simply deallocates the memory and does not drop any values.
	pub struct ErasedXar {
		committed: Cell<usize>,
		inner: NonNull<XarInner>,
	}

	/// Pointer to a value inside an ErasedXar.
	#[repr(transparent)]
	#[derive(Clone, Copy)]
	pub struct ErasedPtr<'a>(NonNull<()>, PhantomData<&'a ()>);
	const _: () = assert!(size_of::<ErasedPtr<'static>>() == size_of::<Option<ErasedPtr<'static>>>());

	impl<'a> ErasedPtr<'a> {
		pub fn from_ptr(ptr: NonNull<()>) -> Self {
			Self(ptr, PhantomData)
		}

		pub fn as_ptr(self) -> NonNull<()> {
			self.0
		}

		#[allow(unused)]
		pub unsafe fn as_ref<T>(self) -> &'a T {
			unsafe{&*self.0.cast::<T>().as_ptr()}
		}

		// ErasedPtr must be valid to write for bytes.len bytes
		// pub unsafe fn write_bytes(self, bytes: &[u8]) {
		// 	unsafe{self.0.as_ptr().cast::<u8>().copy_from(bytes.as_ptr(), bytes.len())}
		// }
		
		/// ErasedPtr must be aligned to `T` and must have atleast enough space
		/// for a `[T]` with `len` elements
		pub unsafe fn write_slice<T: Clone>(self, len: usize, value: T) -> *mut [T] {
			for i in 1..len {
				unsafe{self.0.cast::<T>().add(i).write(value.clone())};
			}
			if len > 0 {
				unsafe{self.0.cast::<T>().write(value)}
			}
			std::ptr::slice_from_raw_parts_mut(self.0.as_ptr().cast::<T>(), len)
		}

		/// ErasedPtr must be aligned to T and must have atleast enough space
		/// for a T
		pub unsafe fn write_value<T>(self, value: T) {
			unsafe{self.0.cast::<T>().write(value)};
		}

		pub unsafe fn detach_lifetime(self) -> ErasedPtr<'static> {
			unsafe{std::mem::transmute::<Self, ErasedPtr<'static>>(self)}
		}

		/// Reads the value as is
		pub unsafe fn read<T>(self) -> T {
			unsafe{self.0.as_ptr().cast::<T>().read()}
		}

		pub unsafe fn byte_add(self, bytes: usize) -> Self {
			unsafe{Self(self.0.byte_add(bytes), PhantomData)}
		}

		// Drops the value at the pointee
		// pub unsafe fn drop_in_place<T>(self) {
		// 	unsafe{self.0.as_ptr().cast::<T>().drop_in_place()}
		// }

		// pub fn as_ptr(self) -> NonNull<()> {
		// 	self.0
		// }
	}

	struct XarInner {
		item_layout: Layout,
		free_list: Cell<Option<ErasedPtr<'static>>>,
		chunks: [Cell<Option<ErasedPtr<'static>>>; 61],
	}

	impl ErasedXar {
		pub fn new(item_layout: Layout) -> Self {
			Self {
				committed: Cell::new(0),
				inner: Self::create_inner(item_layout),
			}
		}

		// /// SAFETY: Layout of T must match the layout used to create the ErasedXar
		// pub unsafe fn insert<T>(&self, value: T) -> ErasedPtr<'_> {
		// 	debug_assert!(Layout::new::<T>() == unsafe{(*self.inner.as_ptr()).item_layout});
		// 	let ret_val = self.get_slot();
		// 	unsafe{ret_val.write_value(value)};
		// 	ret_val
		// }

		pub fn get_slot(&self) -> ErasedPtr<'_> {
			if self.item_size() == 0 {
				return ErasedPtr::from_ptr(NonNull::dangling());
			}
			let inner = unsafe{&*self.inner.as_ptr()};
			if let Some(free) = inner.free_list.get() {
				unsafe{
					let next = free.read::<Option<ErasedPtr>>();
					inner.free_list.set(next);
					free
				}
			} else {
				let location = self.calc_location(self.committed.get());
				let chunk = unsafe{&(*self.inner.as_ptr()).chunks[location.0]};
				let chunk = if let Some(chunk) = chunk.get() {
					chunk
				} else {
					let ret_val = self.alloc_chunk(location.0);
					unsafe{chunk.set(Some(ret_val.detach_lifetime()))};
					ret_val
				};
				let ret_val = unsafe {
					chunk.byte_add(location.1 * self.item_size())
				};
				self.committed.update(|x| x + 1);
				ret_val
			}
		}

		fn item_size(&self) -> usize {
			unsafe{(*self.inner.as_ptr()).item_layout.size()}
		}

		fn item_align(&self) -> usize {
			unsafe{(*self.inner.as_ptr()).item_layout.align()}
		}

		fn first_chunk_size(&self) -> usize {
			let chunk_size = self.item_size();
			if 64 / chunk_size > 8 {64 / chunk_size} else {8}
		}

		fn alloc_chunk(&self, bucket_idx: usize) -> ErasedPtr<'_> {
			let layout = self.chunk_layout(bucket_idx);
			let ptr = unsafe{alloc(layout)}.cast::<()>();
			if ptr.is_null() {
				handle_alloc_error(layout);
			}
			ErasedPtr::from_ptr(unsafe{NonNull::new_unchecked(ptr)})
		}

		fn chunk_size(&self, bucket_idx: usize) -> usize {
			if bucket_idx == 0 || bucket_idx == 1 {
				self.first_chunk_size()
			} else {
				let mut bucket_size = self.first_chunk_size() * 2;
				(2..bucket_idx).for_each(|_| bucket_size *= 2);
				bucket_size
			}
		}

		fn chunk_layout(&self, bucket_idx: usize) -> Layout {
			Layout::from_size_align(self.chunk_size(bucket_idx) * self.item_size(), self.item_align())
				.expect("invalid layout")
		}

		/// Runs a destructor on the pointer and adds its memory to the free list
		///
		/// # SAFETY
		/// handle must be from the current Xar
		pub unsafe fn delete_with<F: FnOnce(ErasedPtr)>(&self, handle: ErasedPtr, f: F) {
			f(handle);
			if self.item_size() == 0 {
				return;
			}
			
			let free_list = unsafe{&(*self.inner.as_ptr()).free_list};
			unsafe{handle.write_value(free_list.get());}
			unsafe{free_list.set(Some(handle.detach_lifetime()))};
		}

		/// Adds the pointer to the free list
		///
		/// # SAFETY
		/// handle must be from the current Xar
		pub unsafe fn delete(&self, handle: ErasedPtr) {
			unsafe{self.delete_with(handle, |_| {})}
		}

		/// Checks if the handle is actually from within self
		pub fn contains(&self, handle: ErasedPtr) -> bool {
			if self.item_size() == 0 {
				return true;
			}
			let inner = unsafe{&*self.inner.as_ptr()};
			let mut current_bucket_size = self.first_chunk_size();
			for bucket in &inner.chunks {
				let Some(bucket) = bucket.get() else {return false};
				if (handle.0.as_ptr().addr()).wrapping_sub(bucket.as_ptr().as_ptr().addr()) < current_bucket_size {
					return true;
				}
				current_bucket_size *= 2;
			}
			false
		}

		fn calc_location(&self, idx: usize) -> (usize, usize) {
			if idx < self.first_chunk_size() {
				(0, idx)
			} else if idx < self.first_chunk_size() * 2 {
				(1, idx - self.first_chunk_size())
			} else {
				let mut bucket = 2;
				let mut bucket_max = self.first_chunk_size() * 4;
				while bucket_max <= idx {
					bucket += 1;
					bucket_max *= 2;
				}
				(bucket, idx - bucket_max / 2)
			}
		}

		fn create_inner(mut item_layout: Layout) -> NonNull<XarInner> {
			let align = std::cmp::max(item_layout.align(), align_of::<Option<ErasedPtr>>());
			let size = std::cmp::max(item_layout.size(), size_of::<Option<ErasedPtr>>());
			item_layout = Layout::from_size_align(size, align)
				.expect("invalid layout");

			let layout = Layout::new::<XarInner>();
			let ptr = unsafe{alloc(layout).cast::<XarInner>()};
			if ptr.is_null() {
				handle_alloc_error(layout);
			}
			// SAFETY: NonNull and aligned
			unsafe {
				ptr.write(XarInner{
					item_layout,
					free_list: Cell::new(None),
					chunks: std::array::from_fn(|_| Cell::new(None)),
				});
			}
			// SAFETY: NonNull
			unsafe{NonNull::new_unchecked(ptr)}
		}

		pub fn clear(&mut self) {
			if self.item_size() != 0 {
				*self.committed.get_mut() = 0;
				unsafe{*(*self.inner.as_ptr()).free_list.get_mut() = None};
			}
		}
	}

	impl Drop for ErasedXar {
		fn drop (&mut self) {
			if self.item_size() == 0 {
				return;
			}
			let inner = unsafe{&mut (*self.inner.as_ptr())};
			for (i, chunk) in inner.chunks.iter_mut().filter_map(|x| x.get_mut().as_mut()).enumerate() {
				let layout = self.chunk_layout(i);
				unsafe{dealloc(chunk.0.as_ptr().cast::<u8>(), layout)};
			}
			let layout = Layout::new::<XarInner>();
			unsafe{dealloc(self.inner.as_ptr().cast::<u8>(), layout)};
		}
	}

	mod trait_impls {
		use super::*;

		use std::fmt::{Debug, Formatter};

		impl Debug for ErasedXar {
			fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
				f.debug_struct("ErasedXar")
					.field("committed", &self.committed.get())
					.field("layout", unsafe{&(*self.inner.as_ptr()).item_layout})
					.finish()
			}
		}

		impl<'a> std::fmt::Debug for ErasedPtr<'a> {
			fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
				f.debug_struct("ErasedPtr")
					.field("ptr", &self.0)
					.finish()
			}
		}
	}

	#[cfg(test)]
	mod test {
		use super::*;
		#[test]
		fn xar_test_1 () {
			let x = ErasedXar::new(Layout::new::<usize>());
			assert_eq!(x.first_chunk_size(), 8);
			assert_eq!(x.calc_location(0), (0, 0));
			assert_eq!(x.calc_location(7), (0, 7));
			assert_eq!(x.calc_location(8), (1, 0));

			assert_eq!(x.calc_location(15), (1, 7));
			assert_eq!(x.calc_location(16), (2, 0));
			assert_eq!(x.calc_location(31), (2, 15));
			assert_eq!(x.calc_location(32), (3, 0));
		}

		#[test]
		fn xar_test_2 () {
			let x = ErasedXar::new(Layout::new::<usize>());
			let x_1 = x.get_slot();
			let x_2 = x.get_slot();
			let x_3 = x.get_slot();
			let x_4 = x.get_slot();
			unsafe{
				x_1.write_value::<usize>(25);
				x_2.write_value::<usize>(26);
				x_3.write_value::<usize>(27);
				x_4.write_value::<usize>(28);
				assert_eq!(25, *x_1.as_ref::<usize>());
				assert_eq!(26, *x_2.as_ref::<usize>());
				assert_eq!(27, *x_3.as_ref::<usize>());
				assert_eq!(28, *x_4.as_ref::<usize>());
				eprintln!("{:?}", x_1.as_ref::<usize>());
			}
		}
	}
}
pub use erased_xar::*;