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 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
//! Provides `Blink` allocator adaptor.
use core::{
alloc::Layout,
convert::{identity, Infallible},
marker::PhantomData,
mem::{needs_drop, size_of, ManuallyDrop, MaybeUninit},
ptr::{self, NonNull},
};
#[cfg(feature = "alloc")]
use allocator_api2::Global;
use crate::{
api::BlinkAllocator,
drop_list::{DropItem, DropList},
in_place,
};
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
use crate::ResultExt;
#[cfg(feature = "alloc")]
use crate::local::BlinkAlloc;
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
use crate::oom::{handle_alloc_error, size_overflow};
type EmplaceType<T, E> = Result<T, ManuallyDrop<E>>;
type EmplaceSlot<T, E> = MaybeUninit<EmplaceType<T, E>>;
pub trait CoerceFromMut<'a, T: ?Sized> {
fn coerce(t: &'a mut T) -> Self;
}
impl<'a, T: ?Sized> CoerceFromMut<'a, T> for &'a mut T {
#[inline(always)]
fn coerce(t: &'a mut T) -> Self {
t
}
}
impl<'a, T: ?Sized> CoerceFromMut<'a, T> for &'a T {
#[inline(always)]
fn coerce(t: &'a mut T) -> Self {
t
}
}
with_global_default! {
/// An allocator adaptor for designed for blink allocator.
/// Provides user-friendly methods to emplace values into allocated memory.
/// Supports emplace existing, constructing value in allocated memory directly or indirectly.
/// And also emplacing items yield from iterator into contiguous memory and returning slice reference.
///
/// [`Blink`] calls [`Drop::drop`] for emplaced values when reset or dropped.
/// This allows to use [`Blink`] instead of collections in some scenarios without needing to enable [`allocation_api`] feature.
///
/// A blink-allocator adapter for user-friendly safe allocations
/// without use of collections.
///
/// Provides an ability to emplace values into allocated memory,
/// baked by the associated blink-allocator instance.
/// And returns mutable reference to the value.
/// Values can be emplaced by move, by construction in allocated memory
/// (when compiler likes us), from iterators etc.
/// Most operations are provided in two flavors:
/// `try_` prefixed methods returns `Result` with allocation errors.
/// And non-prefixed methods calls [`handle_alloc_error`] method
/// (unless "alloc" feature is not enabled, in this case it panics).
/// Non-prefixed methods require "oom_handling" feature (enabled by default)
/// and absence of `no_global_oom_handling` cfg.
///
/// [`Blink`] can be reset by calling `reset` method.
/// It drops all emplaced values and resets associated allocator instance.
/// If allocator instance is shared, resetting it will have no effect.
///
/// [`handle_alloc_error`]: alloc::alloc::handle_alloc_error
/// [`allocation_api`]: https://doc.rust-lang.org/beta/unstable-book/library-features/allocator-api.html
pub struct Blink<A = +BlinkAlloc<Global>> {
drop_list: DropList,
alloc: A,
}
}
impl<A> Default for Blink<A>
where
A: Default,
{
fn default() -> Self {
Blink::new_in(Default::default())
}
}
#[cfg(feature = "alloc")]
impl Blink<BlinkAlloc<Global>> {
/// Creates new blink instance with `BlinkAlloc` baked by `Global`
/// allocator.
///
/// # Examples
///
/// ```
/// # #[cfg(not(feature = "oom_handling"))] fn main() {}
/// # #[cfg(feature = "oom_handling")] fn main() {
/// use blink_alloc::Blink;
/// let mut blink = Blink::new();
///
/// blink.put(42);
/// # }
/// ```
pub const fn new() -> Self {
Blink::new_in(BlinkAlloc::new())
}
/// Creates new blink instance with `BlinkAlloc` baked by `Global`
/// allocator.
/// `BlinkAlloc` receives starting chunk size.
///
/// # Examples
///
/// ```
/// # #[cfg(not(feature = "oom_handling"))] fn main() {}
/// # #[cfg(feature = "oom_handling")] fn main() {
/// use blink_alloc::Blink;
/// let mut blink = Blink::with_chunk_size(16);
///
/// blink.put(42);
/// # }
pub const fn with_chunk_size(capacity: usize) -> Self {
Blink::new_in(BlinkAlloc::with_chunk_size(capacity))
}
}
impl<A> Blink<A> {
/// Creates new blink instance with provided allocator instance.
pub const fn new_in(alloc: A) -> Self {
Blink {
drop_list: DropList::new(),
alloc,
}
}
/// Returns reference to allocator instance.
pub fn inner(&self) -> &A {
&self.alloc
}
/// Drops all allocated values.
///
/// Prefer to use `reset` method if associated allocator instance supports it.
pub fn drop_all(&mut self) {
self.drop_list.reset();
}
}
impl<A> Blink<A>
where
A: BlinkAllocator,
{
/// Drops all allocated values.
/// And resets associated allocator instance.
pub fn reset(&mut self) {
self.drop_list.reset();
self.alloc.reset();
}
/// Allocates memory for a copy of the slice.
/// If allocation fails, returns `Err`.
/// Otherwise copies the slice into the allocated memory and returns
/// mutable reference to the copy.
#[inline]
unsafe fn _try_copy_slice<'a, T, E>(
&'a self,
slice: &[T],
alloc_err: impl FnOnce(Layout) -> E,
) -> Result<&'a mut [T], E>
where
T: Copy,
{
let layout = Layout::for_value(slice);
let Ok(ptr) = self.alloc.allocate(layout) else {
return Err(alloc_err(layout));
};
let ptr = ptr.as_ptr().cast();
core::ptr::copy_nonoverlapping(slice.as_ptr(), ptr, slice.len());
Ok(core::slice::from_raw_parts_mut(ptr, slice.len()))
}
unsafe fn _try_emplace_drop<'a, T, I, G: 'a, E>(
&'a self,
init: I,
f: impl FnOnce(&mut EmplaceSlot<T, G>, I),
err: impl FnOnce(G) -> E,
alloc_err: impl FnOnce(I, Layout) -> E,
) -> Result<&'a mut T, E> {
let layout = Layout::new::<DropItem<Result<T, ManuallyDrop<E>>>>();
let Ok(ptr) = self.alloc.allocate(layout) else {
return Err(alloc_err(init, layout));
};
// Safety: `item_ptr` is a valid pointer to allocated memory for type `DropItem<T>`.
let item = unsafe { DropItem::init_value(ptr.cast(), init, f) };
if item.value.is_ok() {
match self.drop_list.add(item) {
Ok(value) => return Ok(value),
_ => unreachable!(),
}
}
match &mut item.value {
Err(g) => {
let err = err(unsafe { ManuallyDrop::take(g) });
// Give memory back.
self.alloc.deallocate(ptr.cast(), layout);
Err(err)
}
_ => unreachable!(),
}
}
unsafe fn _try_emplace_no_drop<'a, T, I, G: 'a, E>(
&self,
init: I,
f: impl FnOnce(&mut EmplaceSlot<T, G>, I),
err: impl FnOnce(G) -> E,
alloc_err: impl FnOnce(I, Layout) -> E,
) -> Result<&'a mut T, E> {
let layout = Layout::new::<T>();
let Ok(ptr) = self.alloc.allocate(layout) else {
return Err(alloc_err(init, layout));
};
// Safety: `ptr` is a valid pointer to allocated memory.
// Allocated with this `T`'s layout.
// Duration of the allocation is until next call to [`BlinkAlloc::reset`].
let uninit = &mut *ptr.as_ptr().cast();
f(uninit, init);
match uninit.assume_init_mut() {
Ok(value) => Ok(value),
Err(g) => {
let err = err(unsafe { ManuallyDrop::take(g) });
// Give memory back.
self.alloc.deallocate(ptr.cast(), layout);
Err(err)
}
}
}
/// Allocates memory for a value and emplaces value into the memory
/// using init value and provided closure.
/// If allocation fails, returns `Err(init)`.
/// Otherwise calls closure consuming `init`
/// and initializes memory with closure result.
#[inline]
unsafe fn _try_emplace<'a, T, I, G: 'a, E>(
&'a self,
init: I,
f: impl FnOnce(&mut EmplaceSlot<T, G>, I),
no_drop: bool,
err: impl FnOnce(G) -> E,
alloc_err: impl FnOnce(I, Layout) -> E,
) -> Result<&'a mut T, E> {
if !needs_drop::<T>() || no_drop {
self._try_emplace_no_drop(init, f, err, alloc_err)
} else {
self._try_emplace_drop(init, f, err, alloc_err)
}
}
unsafe fn _try_emplace_drop_from_iter<'a, T: 'a, I, E>(
&'a self,
mut iter: I,
err: impl FnOnce(&'a mut [T], T, Option<Layout>) -> E,
) -> Result<&'a mut [T], E>
where
I: Iterator<Item = T>,
{
struct Guard<'a, T: 'a> {
ptr: Option<NonNull<DropItem<[T; 0]>>>,
count: usize,
drop_list: &'a DropList,
}
impl<'a, T> Drop for Guard<'a, T> {
#[inline]
fn drop(&mut self) {
self._flush();
}
}
impl<'a, T> Guard<'a, T> {
#[inline]
fn set(mut self) -> &'a mut [T] {
self._flush()
}
#[inline]
fn _flush(&mut self) -> &'a mut [T] {
if let Some(ptr) = self.ptr.take() {
// Safety: `item` was properly initialized.
let (item, slice) = unsafe { DropItem::init_slice(ptr, self.count) };
unsafe {
self.drop_list.add(item);
}
slice
} else {
&mut []
}
}
}
let mut guard = Guard {
ptr: None,
count: 0,
drop_list: &self.drop_list,
};
let mut last_layout = Layout::new::<()>();
let item_layout = Layout::new::<DropItem<[T; 0]>>();
loop {
let Some(one_more_elem) = iter.next() else {
return Ok(guard.set());
};
let (lower, _) = iter.size_hint();
let Some(lower) = lower.checked_add(1) else {
return Err(err(guard.set(), one_more_elem, None));
};
let Some(mut size_hint) = guard.count.checked_add(lower) else {
return Err(err(guard.set(), one_more_elem, None));
};
let Ok(array_layout) = Layout::array::<T>(size_hint) else {
return Err(err(guard.set(), one_more_elem, None));
};
let Ok((full_layout, array_offset)) = item_layout.extend(array_layout) else {
return Err(err(guard.set(), one_more_elem, None));
};
debug_assert_eq!(array_offset, size_of::<DropItem<[T; 0]>>());
let res = match guard.ptr {
None => self.alloc.allocate(full_layout),
Some(ptr) => self.alloc.grow(ptr.cast(), last_layout, full_layout),
};
let Ok(ptr) = res else {
return Err(err(guard.set(), one_more_elem, Some(full_layout)));
};
last_layout = full_layout;
let item_ptr = ptr.cast();
guard.ptr = Some(item_ptr);
let len = ptr.len();
if len > full_layout.size() {
let fits = (len - size_of::<DropItem<[T; 0]>>()) / size_of::<T>();
size_hint = fits;
} else {
debug_assert_eq!(len, full_layout.size());
}
let array_ptr = unsafe { item_ptr.as_ptr().add(1).cast::<T>() };
// Safety: `array_ptr` is a valid pointer to allocated memory for type `[T; hint]`.
// And `hint` is larger than `guard.count`
unsafe {
ptr::write(array_ptr.add(guard.count), one_more_elem);
}
guard.count += 1;
for idx in guard.count..size_hint {
if Layout::new::<Option<T>>() == Layout::new::<T>() {
// Putting elements directly into the array.
if in_place(array_ptr.add(idx).cast(), &mut iter, Iterator::next).is_none() {
break;
}
} else {
match iter.next() {
None => break,
Some(elem) => ptr::write(array_ptr.add(idx), elem),
}
}
guard.count = idx + 1;
}
}
}
unsafe fn _try_emplace_no_drop_from_iter<'a, T: 'a, I, E>(
&'a self,
mut iter: I,
err: impl FnOnce(&'a mut [T], T, Option<Layout>) -> E,
) -> Result<&'a mut [T], E>
where
I: Iterator<Item = T>,
{
struct Guard<T> {
ptr: Option<NonNull<T>>,
count: usize,
}
impl<T> Guard<T> {
#[inline]
fn set<'a>(self) -> &'a mut [T] {
match self.ptr {
Some(ptr) => {
// Safety: `slice` was properly initialized.
unsafe { core::slice::from_raw_parts_mut(ptr.as_ptr(), self.count) }
}
None => &mut [],
}
}
}
let mut guard = Guard {
ptr: None,
count: 0,
};
let mut last_layout = Layout::new::<()>();
loop {
let Some(one_more_elem) = iter.next() else {
return Ok(guard.set());
};
let (lower, _) = iter.size_hint();
let Some(lower) = lower.checked_add(1) else {
return Err(err(guard.set(), one_more_elem, None));
};
let Some(mut size_hint) = guard.count.checked_add(lower) else {
return Err(err(guard.set(), one_more_elem, None));
};
let Ok(array_layout) = Layout::array::<T>(size_hint) else {
return Err(err(guard.set(), one_more_elem, None));
};
let res = match guard.ptr {
None => self.alloc.allocate(array_layout),
Some(ptr) => self.alloc.grow(ptr.cast(), last_layout, array_layout),
};
let Ok(ptr) = res else {
return Err(err(guard.set(), one_more_elem, Some(array_layout)));
};
last_layout = array_layout;
guard.ptr = Some(ptr.cast());
let len = ptr.len();
if len > array_layout.size() {
let fits = len / size_of::<T>();
debug_assert!(fits >= size_hint);
size_hint = fits;
} else {
debug_assert_eq!(len, array_layout.size());
}
let array_ptr = ptr.as_ptr().cast::<T>();
// Safety: `array_ptr` is a valid pointer to allocated memory for type `[T; hint]`.
// And `hint` is larger than `guard.count`
unsafe {
ptr::write(array_ptr.add(guard.count), one_more_elem);
}
guard.count += 1;
for idx in guard.count..size_hint {
if Layout::new::<Option<T>>() == Layout::new::<T>() {
// Putting elements directly into the array.
if in_place(array_ptr.add(idx).cast(), &mut iter, Iterator::next).is_none() {
break;
}
} else {
match iter.next() {
None => break,
Some(elem) => ptr::write(array_ptr.add(idx), elem),
}
}
guard.count = idx + 1;
}
}
}
/// Allocates memory for a value and emplaces value into the memory
/// using init value and provided closure.
/// If allocation fails, returns `Err(init)`.
/// Otherwise calls closure consuming `init`
/// and initializes memory with closure result.
#[inline]
unsafe fn _try_emplace_from_iter<'a, T: 'a, I, E>(
&'a self,
iter: I,
no_drop: bool,
err: impl FnOnce(&'a mut [T], T, Option<Layout>) -> E,
) -> Result<&mut [T], E>
where
I: IntoIterator<Item = T>,
{
if !needs_drop::<T>() || no_drop {
self._try_emplace_no_drop_from_iter(iter.into_iter(), err)
} else {
self._try_emplace_drop_from_iter(iter.into_iter(), err)
}
}
}
/// Provides interface for emplacing values.
/// Created by [`Blink::emplace`], [`Blink::emplace_no_drop`]
/// and [`Blink::emplace_unchecked`].
pub struct Emplace<'a, A, T, R = &'a mut T, S = &'a mut [T]> {
blink: &'a Blink<A>,
no_drop: bool,
marker: PhantomData<fn(T) -> (R, S)>,
}
impl<'a, A, T, R, S> Emplace<'a, A, T, R, S>
where
A: BlinkAllocator,
T: 'a,
R: CoerceFromMut<'a, T>,
S: CoerceFromMut<'a, [T]>,
{
/// Allocates memory for a value and moves `value` into the memory.
/// If allocation fails, returns `Err(value)`.
/// On success returns reference to the emplaced value.
#[inline]
pub fn try_value(&self, value: T) -> Result<R, T> {
unsafe {
self.blink._try_emplace(
value,
|slot, value| {
slot.write(Ok::<_, ManuallyDrop<Infallible>>(value));
},
self.no_drop,
|never| match never {},
|init, _| init,
)
}
.map(R::coerce)
}
/// Allocates memory for a value and moves `value` into the memory.
/// Returns reference to the emplaced value.
/// If allocation fails, diverges.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
pub fn value(&self, value: T) -> R {
R::coerce(
unsafe {
self.blink._try_emplace(
value,
|slot, value| {
slot.write(Ok::<_, ManuallyDrop<Infallible>>(value));
},
self.no_drop,
identity,
|_, layout| handle_alloc_error(layout),
)
}
.safe_ok(),
)
}
/// Allocates memory for a value.
/// On success invokes closure and initialize the value.
/// Returns reference to the value.
/// If allocation fails, returns error with closure.
#[inline]
pub fn try_with<F>(&self, f: F) -> Result<R, F>
where
F: FnOnce() -> T,
{
unsafe {
self.blink._try_emplace(
f,
|slot, f| {
slot.write(Ok::<_, ManuallyDrop<Infallible>>(f()));
},
self.no_drop,
never,
|f, _| f,
)
}
.map(R::coerce)
}
/// Allocates memory for a value.
/// On success invokes closure and initialize the value.
/// Returns reference to the value.
/// If allocation fails, diverges.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
pub fn with<F>(&self, f: F) -> R
where
F: FnOnce() -> T,
{
R::coerce(
unsafe {
self.blink._try_emplace(
f,
|slot, f| {
slot.write(Ok::<_, ManuallyDrop<Infallible>>(f()));
},
self.no_drop,
never,
|_, layout| handle_alloc_error(layout),
)
}
.safe_ok(),
)
}
/// Allocates memory for a value.
/// If allocation fails, returns error with closure.
/// On success invokes closure and initialize the value.
/// If closure fails, returns the error.
/// Returns reference to the value.
#[inline]
pub fn try_with_fallible<F, E>(&self, f: F) -> Result<R, Result<E, F>>
where
F: FnOnce() -> Result<T, E>,
E: 'a,
{
unsafe {
self.blink._try_emplace(
f,
|slot, f| {
slot.write(f().map_err(ManuallyDrop::new));
},
self.no_drop,
|err| Ok(err),
|f, _| Err(f),
)
}
.map(R::coerce)
}
/// Allocates memory for a value.
/// If allocation fails, returns error with closure.
/// On success invokes closure and initialize the value.
/// If closure fails, returns the error.
/// Returns reference to the value.
#[inline]
pub fn with_fallible<F, E>(&self, f: F) -> Result<R, E>
where
F: FnOnce() -> Result<T, E>,
E: 'a,
{
unsafe {
self.blink._try_emplace(
f,
|slot, f| {
slot.write(f().map_err(ManuallyDrop::new));
},
self.no_drop,
identity,
|_, layout| handle_alloc_error(layout),
)
}
.map(R::coerce)
}
/// Allocates memory for an array and initializes it with
/// values from iterator.
/// Uses iterator hints to allocate memory.
/// If iterator yields more values than allocated array can hold,
/// grows allocation and moves next values to extended array.
/// Repeats until iterator is exhausted.
/// Works best on iterators that report accurate upper size hint.
/// Grows allocated memory potentially reducing number of allocations
/// and copies.
/// If allocation fails, returns slice of values emplaced so far.
/// And one element that was taken from iterator and not emplaced.
#[inline]
pub fn try_from_iter<I>(&self, iter: I) -> Result<S, (S, T)>
where
I: IntoIterator<Item = T>,
{
unsafe {
self.blink
._try_emplace_from_iter(iter, self.no_drop, |slice: &'a mut [T], value, _| {
(S::coerce(slice), value)
})
}
.map(S::coerce)
}
/// Allocates memory for an array and initializes it with
/// values from iterator.
/// Uses iterator hints to allocate memory.
/// If iterator yields more values than allocated array can hold,
/// grows allocation and moves next values to extended array.
/// Repeats until iterator is exhausted.
/// Works best on iterators that report accurate upper size hint.
/// Grows allocated memory potentially reducing number of allocations
/// and copies.
/// If allocation fails, diverges.
/// Values already emplaced will be dropped.
/// One last value that was taken from iterator and not emplaced
/// is dropped before this method returns.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
pub fn from_iter<I>(&self, iter: I) -> S
where
I: Iterator<Item = T>,
{
S::coerce(
unsafe {
self.blink
._try_emplace_from_iter(iter, self.no_drop, |_, _, layout| match layout {
Some(layout) => handle_alloc_error(layout),
None => size_overflow(),
})
}
.safe_ok(),
)
}
}
impl<A> Blink<A>
where
A: BlinkAllocator,
{
/// Puts value into this `Blink` instance.
/// Returns reference to the value.
///
/// Effectively extends lifetime of the value
/// from local scope to the reset scope.
///
/// For more flexible value placement see
/// [`Blink::emplace`], [`Blink::emplace_no_drop`] and
/// [`Blink::emplace_unchecked`].
///
/// # Example
///
/// ```
/// # use blink_alloc::Blink;
/// let mut blink = Blink::new();
/// let foo = blink.put(42);
/// assert_eq!(*foo, 42);
/// *foo = 24;
/// blink.reset();
/// // assert_eq!(*foo, 24); // Cannot compile. `foo` does not outlive reset.
/// ```
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
pub fn put<T: 'static>(&self, value: T) -> &mut T {
self.emplace().value(value)
}
/// Allocates memory for a value.
/// Returns some reference to the uninitialized value.
/// If allocation fails, returns none.
#[inline]
pub fn try_uninit<T>(&self) -> Option<&mut MaybeUninit<T>> {
let layout = Layout::new::<T>();
let ptr = self.alloc.allocate(layout).ok()?;
// Safety:
// - `ptr` is valid for `layout`.
// - `MaybeUninit` is always initialized.
Some(unsafe { &mut *ptr.as_ptr().cast() })
}
/// Allocates memory for a value.
/// Returns reference to the uninitialized value.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn uninit<T>(&self) -> &mut MaybeUninit<T> {
let layout = Layout::new::<T>();
let ptr = self
.alloc
.allocate(layout)
.unwrap_or_else(|_| handle_alloc_error(layout));
// Safety:
// - `ptr` is valid for `layout`.
// - `MaybeUninit` is always initialized.
unsafe { &mut *ptr.as_ptr().cast() }
}
/// Copies the slice to the allocated memory
/// and returns reference to the new slice.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn copy_slice<T>(&self, slice: &[T]) -> &mut [T]
where
T: Copy,
{
let result = unsafe { self._try_copy_slice(slice, handle_alloc_error) };
match result {
Ok(slice) => slice,
Err(never) => never,
}
}
/// Allocates memory for a copy of the slice.
/// Copies the slice to the allocated memory
/// and returns reference to the new slice.
/// If allocation fails, returns `None`.
#[inline]
pub fn try_copy_slice<T>(&self, slice: &[T]) -> Option<&mut [T]>
where
T: Copy,
{
unsafe { self._try_copy_slice(slice, |_| ()) }.ok()
}
/// Copies the slice to the allocated memory
/// and returns reference to the new slice.
#[cfg(all(feature = "oom_handling", not(no_global_oom_handling)))]
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn copy_str(&self, string: &str) -> &mut str {
let result = unsafe { self._try_copy_slice(string.as_bytes(), handle_alloc_error) };
match result {
Ok(slice) => unsafe { core::str::from_utf8_unchecked_mut(slice) },
Err(never) => never,
}
}
/// Allocates memory for a copy of the slice.
/// Copies the slice to the allocated memory
/// and returns reference to the new slice.
/// If allocation fails, returns `None`.
#[inline]
pub fn try_copy_str(&self, string: &str) -> Option<&mut str> {
unsafe { self._try_copy_slice(string.as_bytes(), |_| ()) }
.ok()
.map(|bytes| unsafe { core::str::from_utf8_unchecked_mut(bytes) })
}
/// Returns an `Emplace` adaptor that can emplace values into
/// the blink allocator.
///
/// This version requires the value type to be `'static`.
/// To use with non-static types consider using one of the following:
///
/// * [`Blink::emplace_no_drop`]
/// Causes emplaced value to not be dropped on reset.
/// Avoiding potential unsoundness in `Drop` implementation.
/// * [`Blink::emplace_shared`]
/// Returns shared reference to emplaced values.
/// * [`Blink::emplace_unchecked`]
/// Unsafe version of `emplace`.
/// User must guarantee that the value won't have access to references
/// allocated by the blink allocator later.
///
/// # Example
///
/// ```
/// # use blink_alloc::Blink;
/// let mut blink = Blink::new();
/// let foo = blink.put(42);
/// assert_eq!(*foo, 42);
/// *foo = 24;
/// blink.reset();
/// // assert_eq!(*foo, 24); // Cannot compile. `foo` does not outlive reset.
/// ```
#[inline]
pub fn emplace<T: 'static>(&self) -> Emplace<A, T> {
Emplace {
blink: self,
no_drop: false,
marker: PhantomData,
}
}
/// Returns an `Emplace` adaptor that can emplace values into
/// the blink allocator.
///
/// This version causes emplaced value to be not-dropped on reset.
/// To drop returned value on reset, consider one of the following:
///
/// * [`Blink::emplace`]
/// Requires the value type to be `'static`.
/// * [`Blink::emplace_shared`]
/// Returns shared reference to emplaced values.
/// * [`Blink::emplace_unchecked`]
/// Unsafe version of `emplace`.
/// User must guarantee that the value won't have access to references
/// allocated by the blink allocator later.
///
/// # Example
///
/// ```
/// # use blink_alloc::Blink;
/// struct Foo<'a>(&'a String);
///
/// impl Drop for Foo<'_> {
/// fn drop(&mut self) {
/// println!("{}", self.0);
/// }
/// }
///
/// let mut blink = Blink::new();
/// let s = "Hello".to_owned();
/// let foo = blink.emplace_no_drop().value(Foo(&s));
/// assert_eq!(foo.0, "Hello");
/// let world = blink.put("World".to_owned());
/// // Would be unsound if `foo` could be dropped.
/// foo.0 = world;
/// blink.reset();
/// // assert_eq!(foo.0, "Universe"); // Cannot compile. `foo` does not outlive reset.
/// ```
#[inline]
pub fn emplace_no_drop<T>(&self) -> Emplace<A, T> {
Emplace {
blink: self,
no_drop: true,
marker: PhantomData,
}
}
/// Returns an `Emplace` adaptor that can emplace values into
/// the blink allocator.
///
/// This version returns shared references to emplaced values.
/// Lifts the `'static` requirement.
/// Still allows emplaced values to be dropped on reset.
///
/// To drop returned value on reset, consider one of the following:
///
/// * [`Blink::emplace`]
/// Requires the value type to be `'static`.
/// * [`Blink::emplace_no_drop`]
/// Causes emplaced value to not be dropped on reset.
/// Avoiding potential unsoundness in `Drop` implementation.
/// * [`Blink::emplace_unchecked`]
/// Unsafe version of `emplace`.
/// User must guarantee that the value won't have access to references
/// allocated by the blink allocator later.
///
///
/// ```
/// # use blink_alloc::Blink;
/// struct Foo<'a>(&'a String);
///
/// impl Drop for Foo<'_> {
/// fn drop(&mut self) {
/// println!("{}", self.0);
/// }
/// }
///
/// let mut blink = Blink::new();
/// let s = "Hello".to_owned();
/// let foo = blink.emplace_no_drop().value(Foo(&s));
/// assert_eq!(foo.0, "Hello");
/// let world = blink.put("World".to_owned());
/// // Would be unsound if `foo` was mutable.
/// // foo.0 = world;
/// blink.reset();
/// // assert_eq!(foo.0, "Universe"); // Cannot compile. `foo` does not outlive reset.
/// ```
#[inline]
pub fn emplace_shared<T>(&self) -> Emplace<A, T, &T, &[T]> {
Emplace {
blink: self,
no_drop: true,
marker: PhantomData,
}
}
/// Returns an `Emplace` adaptor that can emplace values into
/// the blink allocator.
///
/// This is unsafe version of [`Blink::emplace`].
/// User must guarantee that values won't attempt to access
/// memory allocated by the blink allocator later in their [`Drop::drop`]
/// For safe code consider using one of the following:
///
/// * [`Blink::emplace`]
/// Requires the value type to be `'static`.
/// * [`Blink::emplace_no_drop`]
/// Causes emplaced value to not be dropped on reset.
/// Avoiding potential unsoundness in `Drop` implementation.
/// * [`Blink::emplace_shared`]
/// Returns shared reference to emplaced values.
///
/// # Safety
///
/// Avoid incorrect usage. See below.
///
/// # Incorrect usage example
///
/// Other emplace methods are safe as they guarantee following case
/// is impossible.
///
/// ```no_run
/// # use blink_alloc::Blink;
/// struct Foo<'a>(&'a String);
///
/// impl Drop for Foo<'_> {
/// fn drop(&mut self) {
/// println!("{}", self.0);
/// }
/// }
///
/// let mut blink = Blink::new();
/// let s = "Hello".to_owned();
/// let foo = blink.emplace_no_drop().value(Foo(&s));
/// assert_eq!(foo.0, "Hello");
/// let world = blink.put("World".to_owned());
/// // Unsound since `foo` would access `world` in `Drop`
/// // and `world` is dropped earlier.
/// foo.0 = world;
/// blink.reset();
/// // assert_eq!(foo.0, "Universe"); // Cannot compile. `foo` does not outlive reset.
/// ```
#[inline]
pub unsafe fn emplace_unchecked<T>(&self) -> Emplace<A, T> {
Emplace {
blink: self,
no_drop: false,
marker: PhantomData,
}
}
}
/// Wrapper for [`Blink`] that implements [`Send`].
///
/// Normally it is impossible to send [`Blink`] to another thread
/// due to the fact that it will drop non-sendable types on reset.
///
/// This wrapper resets [`Blink`] on construction and thus safe to send.
///
/// # Example
///
/// ```
/// # use blink_alloc::{SendBlink, Blink};
/// let mut blink = Blink::new();
/// let rc = std::rc::Rc::new(42);
/// let rc = blink.put(rc);
/// assert_eq!(**rc, 42);
/// let send_blink = SendBlink::new(blink);
///
/// std::thread::scope(move |_| {
/// let mut blink = send_blink.into_inner();
/// blink.put(42);
/// });
/// ````
pub struct SendBlink<A> {
blink: Blink<A>,
}
impl<A> SendBlink<A>
where
A: BlinkAllocator,
{
/// Creates new [`SendBlink`] from [`Blink`].
/// Resets the blink allocator to avoid dropping non-sendable types on other threads.
pub fn new(mut blink: Blink<A>) -> Self {
blink.reset();
SendBlink { blink }
}
/// Returns inner [`Blink`] value.
pub fn into_inner(self) -> Blink<A> {
self.blink
}
}
#[inline]
fn never<T>(never: Infallible) -> T {
match never {}
}