You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of #1478 and #1658, allocation failures in our API are returned as Result::Err rather than being handled by aborting/panicking (e.g. via handle_alloc_error). This is maximally flexible, but introduces an ergonomics burden to users who prefer the old behavior.
Here are two possible designs to make this more ergonomic.
Provide handle_alloc_error on Result<T, AllocError>
...a caller with a r: Result<T, AllocError> can just call r.unwrap_or_handle_alloc_error().
The downside to this approach is that handle_alloc_error requires a Layout argument. In order to supply this, we'd need to have AllocError carry a Layout, which would make our Results larger and add optimizer pressure.
Parametrize over the error type
We make allocation errors generic with an AllocError trait which can be constructed from a Layout. We also implement this trait for ! - this implementation diverges by calling handle_alloc_error when constructed.
A caller can write try_reserve::<_, !>(v, additional).into_ok() to recover the infallible behavior or try_reserve::<_, AllocErr>(v, additional) to keep the fallible behavior.
This has a number of downsides:
Since type defaults are not supported on functions, the caller must always specify the error type at the call site
We cannot always synthesize a valid Layout for a failed allocation (e.g., this try_reserve function could fail because the user passed an additional that would cause the Vec's size to overflow isize - this cannot be represented using Layout). As a result, we need from_failed_allocation to accept Layouts that might be basically nonsense placeholders like we pass from try_reserve.
The text was updated successfully, but these errors were encountered:
See also: #528
As of #1478 and #1658, allocation failures in our API are returned as
Result::Err
rather than being handled by aborting/panicking (e.g. viahandle_alloc_error
). This is maximally flexible, but introduces an ergonomics burden to users who prefer the old behavior.Here are two possible designs to make this more ergonomic.
Provide
handle_alloc_error
onResult<T, AllocError>
Using this extension trait:
...a caller with a
r: Result<T, AllocError>
can just callr.unwrap_or_handle_alloc_error()
.The downside to this approach is that
handle_alloc_error
requires aLayout
argument. In order to supply this, we'd need to haveAllocError
carry aLayout
, which would make ourResult
s larger and add optimizer pressure.Parametrize over the error type
We make allocation errors generic with an
AllocError
trait which can be constructed from aLayout
. We also implement this trait for!
- this implementation diverges by callinghandle_alloc_error
when constructed.A caller can write
try_reserve::<_, !>(v, additional).into_ok()
to recover the infallible behavior ortry_reserve::<_, AllocErr>(v, additional)
to keep the fallible behavior.This has a number of downsides:
Layout
for a failed allocation (e.g., thistry_reserve
function could fail because the user passed anadditional
that would cause theVec
's size to overflowisize
- this cannot be represented usingLayout
). As a result, we needfrom_failed_allocation
to acceptLayout
s that might be basically nonsense placeholders like we pass fromtry_reserve
.The text was updated successfully, but these errors were encountered: