Re: [PATCH] rust: alloc: allow different error types in `KBox::pin_slice`
From: Gary Guo
Date: Fri Feb 27 2026 - 09:40:01 EST
On Sat Feb 14, 2026 at 2:56 PM GMT, Danilo Krummrich wrote:
> On Sat Feb 14, 2026 at 3:40 PM CET, Benno Lossin wrote:
>> 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.
>
> Yeah, but isn't this only because of [1], which does not apply to the kernel
> because our minimum compiler version is 1.78 anyways?
>
> I.e. are there any cases where we can't implement From in the kernel and have to
> fall back to Into?
>
> [1] https://doc.rust-lang.org/std/convert/trait.Into.html#implementing-into-for-conversions-to-external-types-in-old-versions-of-rust
There's one benefit in using `From` in trait bound -- you can call both `From::from`
and `Into::into` inside the function. If you only have `Into` bound, then
`From::from` is not callable. A very minor benefit, though.
Another interesting observation is that `?` operator (i.e. impl of the unstable
`FromResidual` trait on `Result`) uses `From` instead of `Into`. I cannot find a
reason why this is done this way, though.
Best,
Gary