Re: [PATCH] rust: alloc: allow different error types in `KBox::pin_slice`

From: Benno Lossin

Date: Sat Feb 14 2026 - 09:40:33 EST


On Sat Feb 14, 2026 at 3:17 PM CET, Danilo Krummrich wrote:
> On Sat Feb 14, 2026 at 2:28 PM CET, Andreas Hindborg wrote:
>> @@ -333,24 +333,31 @@ pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
>> /// assert_eq!(s[3].d.lock().a, 20);
>> /// # Ok::<(), Error>(())
>> /// ```
>> - pub fn pin_slice<Func, Item, E>(
>> + pub fn pin_slice<Func, Item, E, E2>(
>> mut init: Func,
>> len: usize,
>> flags: Flags,
>> ) -> Result<Pin<Box<[T], A>>, E>
>> where
>> Func: FnMut(usize) -> Item,
>> - Item: PinInit<T, E>,
>> - E: From<AllocError>,
>
> I think we should keep this bound and just add:

The `Into` trait bounds are the idiomatic ones for functions consuming
things. See https://doc.rust-lang.org/std/convert/trait.Into.html:

Prefer using Into over From when specifying trait bounds on a
generic function to ensure that types that only implement Into can
be used as well.

I should've said something to Andreas, since he created the commit
message (but this patch has left my L3 cache).

Cheers,
Benno

> E: From<E2>,
>
>> + Item: PinInit<T, E2>,
>> + AllocError: Into<E>,
>> + E2: Into<E>,
>> {
>> - let mut buffer = super::Vec::<T, A>::with_capacity(len, flags)?;
>> + let mut buffer = match super::Vec::<T, A>::with_capacity(len, flags) {
>> + Ok(buffer) => buffer,
>> + Err(err) => return Err(err.into()),
>> + };
>
> This...
>
>> for i in 0..len {
>> let ptr = buffer.spare_capacity_mut().as_mut_ptr().cast();
>> // SAFETY:
>> // - `ptr` is a valid pointer to uninitialized memory.
>> // - `ptr` is not used if an error is returned.
>> // - `ptr` won't be moved until it is dropped, i.e. it is pinned.
>> - unsafe { init(i).__pinned_init(ptr)? };
>> + match unsafe { init(i).__pinned_init(ptr) } {
>> + Ok(()) => (),
>> + Err(err) => return Err(err.into()),
>> + }
>
> ...and this match becomes unnecessary then.