Open brandenburg opened 3 years ago
You're totally right, it is really old code I forgot to remove when I published the crate. Apologies..
I have no idea neither, appart from using really unsafe code to create the pointer to give to Arc::from_raw. For the moment we should remove this method.
Thanks for the quick confirmation! That's unfortunate, though; I was hoping to use the panic-free Arc
. The only option (other than evil, brittle pointer hacks) I see now is to simply re-implement Arc
from scratch (or copy&paste&adjust code from alloc
crate), which is a bit unfortunate.
This can probably be closed since try_new exists on Arc nowadays (https://doc.rust-lang.org/std/sync/struct.Arc.html#method.try_new)
try_new_uninit_slice
is missing, so fallible Arc
of slices (Arc<[_]>
) is still impossible to do (https://github.com/rust-lang/rust/issues/63291)
Hi, doesn't this apply to Rc
as well? The code path looks very similar when doing Rc::from(Box<T>)
in this crate's Rc::try_new
.
Yes, same for Rc
I was trying to understand how
FallibleArc::try_new
is implemented and now wonder whether it works at all.My expectation is that
FallibleArc::try_new()
should returnErr(TryReserveError)
if the allocator runs out of memory at any point while executingFallibleArc::try_new()
. However, I think it can still panic. Here's the relevant code:(Aside: I do not understand what the comment "doesn't work as the inner variable of arc are also stocked in the box" is supposed to convey — my apologies if this duplicates a known issue.)
Suppose the
Box::try_new()
succeeds, but it exhausts the memory allocator (i.e., any future allocation attempt will fail). ThenArc::from(b)
callsArc::from_box()
:Arc::from_box()
callsArc::allocate_for_ptr()
, which is just a wrapper aroundArc::allocate_for_layout()
.Let's take a look at
Arc::allocate_for_layout()
:Note the line
allocate(layout).unwrap_or_else(|_| handle_alloc_error(layout))
— if the allocator runs out of memory at this point, the process will panic "as usual", defeating the purpose of usingtry_new
in the first place.Am I missing something? If not, this corner case should probably mentioned in the documentation.
Is there a good way to ensure that
Arc::try_new
will truly never panic? SinceArcInner
is not public, I don't see a good way to construct an allocation that would be suitable for use withArc::from_raw()
.