Message ID | 20221202161502.385525-28-ojeda@kernel.org |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp949128wrr; Fri, 2 Dec 2022 08:21:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf6SoNqSejFImVUgSqd+LNLfNPo+/aq1aOrAXlg5swC/ZKU8SuQgWl/D7IPGV/YITzKsS2P2 X-Received: by 2002:a17:903:3011:b0:189:ac81:54d3 with SMTP id o17-20020a170903301100b00189ac8154d3mr12806738pla.130.1669998067789; Fri, 02 Dec 2022 08:21:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669998067; cv=none; d=google.com; s=arc-20160816; b=mBeq3a8ZiuVhuRA144VB/JgNFWTv24vrGqe2n+syCmvUDR7ugHK+mMYkorR7OwnuIA xyQm5FXiF+eH2VsquAJpb93oy1v6T570K7DV5xHjne/DoqgZieNSIjYsxlsRReRtNiYa +P5eRuU42sx0Pco1XWB1W2/x/fTxHPZ6YqiU+/imfeiyd6AdESoNMUdSiyRINy7PQ/gl /TCkJbmT2uBKe1DMiR7U+tDZJYs2TSWooFZgJX/nhOPRDkEozV2PAoBRgYPoNB7PQWQ9 GiuEZIJyKCLLLfUGPqyaAsh/w7+7Q2gFYTOjgG0eXflu9l9+qAMVqjy1tkKaCxCqUa8P rrNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=H+qIDzozhgpQC8XQ4TzwJGrLT45xetRrm2RvjeIc7OA=; b=x10okwMcrXSdCUQB6QZKI0THpbLVvP8C2iX13H9wpN2bAzAkghQUUYsT081oyAUrJD 8nUFRfm7pW8/I2skUIuSTO6WDBHDRVmGGcz2pjPLxDLAWib/J+sBgKabbXOv5Mwa+QQg Kj6sRdbM4mBrc85cDKdy0lQXb1Z5Fbw43znd0WsFjpBpHudguDAX9UQWOzcfotew0X9y b2MVjxlA8oFxLfx825Mo+63F0knR7uGzkEP0DGkRqJWNeeALRh0kJPDpKr/3yyKt+vIk qa/Q7B8jhj8Wy24Awa8eos63oDeU6YGZ7kerY8OoWLeNuBkoV36Xrzaae8MVh49TJhMy o2dg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KYWxTLoM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g11-20020a65580b000000b00477cd4f65fasi7539868pgr.197.2022.12.02.08.20.53; Fri, 02 Dec 2022 08:21:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=KYWxTLoM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233901AbiLBQSx (ORCPT <rfc822;lhua1029@gmail.com> + 99 others); Fri, 2 Dec 2022 11:18:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234034AbiLBQSL (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Fri, 2 Dec 2022 11:18:11 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03636DA216; Fri, 2 Dec 2022 08:16:47 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9DC07B821EF; Fri, 2 Dec 2022 16:16:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1A092C433C1; Fri, 2 Dec 2022 16:16:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669997804; bh=g8YHlGQGI5+kOudmGxtQLpBJJ0VXh/b1x75GjPjycds=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KYWxTLoMH80ob3tE9WkideVGBL6HUpVaeZ5ha7q+gWJ8kjI8kW66x0wMfXqBvNl+s PJQd5K5KQZRPbx0Sf1colbtpMw5EMb5frdHCbG7WXSId49CmBIqnhXOgw/SvWlMLZW /pZ+eLV9PZFDyxqIlZn7RE/zRC50mbZ565dHUtozufFIjPr8JhTyW/GjdODiw5zipj PX+JcqHJ1LzyhAC0rPUsYW4UiIwEtvsY0IeeGfNCYu7LMAw735b5jv3CF0bl//grQn tO5nU021aoxzEyOal7ng3ohpzMfzsMpB4YvPYEu74tLUPOY8AjYGByia8fsEgC3mBx 4QsQIkCbNju3g== From: ojeda@kernel.org To: Miguel Ojeda <ojeda@kernel.org>, Wedson Almeida Filho <wedsonaf@gmail.com>, Alex Gaynor <alex.gaynor@gmail.com>, Boqun Feng <boqun.feng@gmail.com>, Gary Guo <gary@garyguo.net>, =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= <bjorn3_gh@protonmail.com> Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Wei Liu <wei.liu@kernel.org> Subject: [PATCH v2 27/28] rust: types: add `Either` type Date: Fri, 2 Dec 2022 17:14:58 +0100 Message-Id: <20221202161502.385525-28-ojeda@kernel.org> In-Reply-To: <20221202161502.385525-1-ojeda@kernel.org> References: <20221202161502.385525-1-ojeda@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751119893879012071?= X-GMAIL-MSGID: =?utf-8?q?1751119893879012071?= |
Series |
[v2,01/28] rust: prelude: split re-exports into groups
|
|
Commit Message
Miguel Ojeda
Dec. 2, 2022, 4:14 p.m. UTC
From: Wedson Almeida Filho <wedsonaf@gmail.com> Introduce the new `types` module of the `kernel` crate with `Either` as its first type. `Either<L, R>` is a sum type that always holds either a value of type `L` (`Left` variant) or `R` (`Right` variant). For instance: struct Executor { queue: Either<BoxedQueue, &'static Queue>, } Signed-off-by: Wedson Almeida Filho <wedsonaf@gmail.com> Reviewed-by: Wei Liu <wei.liu@kernel.org> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <ojeda@kernel.org> --- rust/kernel/lib.rs | 1 + rust/kernel/types.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 rust/kernel/types.rs
Comments
On Fri, Dec 02, 2022 at 05:14:58PM +0100, ojeda@kernel.org wrote: > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > Introduce the new `types` module of the `kernel` crate with > `Either` as its first type. > > `Either<L, R>` is a sum type that always holds either a value > of type `L` (`Left` variant) or `R` (`Right` variant). > > For instance: > > struct Executor { > queue: Either<BoxedQueue, &'static Queue>, > } This specific example seems like it would be better served by the existing `Cow` type.
On Sat, Dec 3, 2022 at 12:42 AM Josh Triplett <josh@joshtriplett.org> wrote: > > This specific example seems like it would be better served by the > existing `Cow` type. Yeah, possibly -- it is taken from one of the use cases in the full repository. Perhaps it was deemed simpler, or providing a way to go to an owned instance was to be avoided. Thanks for taking a look! Cheers, Miguel
On Fri, 2 Dec 2022 15:41:59 -0800 Josh Triplett <josh@joshtriplett.org> wrote: > On Fri, Dec 02, 2022 at 05:14:58PM +0100, ojeda@kernel.org wrote: > > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > > > Introduce the new `types` module of the `kernel` crate with > > `Either` as its first type. > > > > `Either<L, R>` is a sum type that always holds either a value > > of type `L` (`Left` variant) or `R` (`Right` variant). > > > > For instance: > > > > struct Executor { > > queue: Either<BoxedQueue, &'static Queue>, > > } > > This specific example seems like it would be better served by the > existing `Cow` type. We use `no_global_oom_handling`, which gates most `ToOwned` implementations (e.g. `str` cannot implement `to_owned()` because it cannot guarantee allocation success). So the Rust `Cow` is pretty much useless in the kernel. Best, Gary
On Sun, 4 Dec 2022 at 10:31, Gary Guo <gary@garyguo.net> wrote: > > On Fri, 2 Dec 2022 15:41:59 -0800 > Josh Triplett <josh@joshtriplett.org> wrote: > > > On Fri, Dec 02, 2022 at 05:14:58PM +0100, ojeda@kernel.org wrote: > > > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > > > > > Introduce the new `types` module of the `kernel` crate with > > > `Either` as its first type. > > > > > > `Either<L, R>` is a sum type that always holds either a value > > > of type `L` (`Left` variant) or `R` (`Right` variant). > > > > > > For instance: > > > > > > struct Executor { > > > queue: Either<BoxedQueue, &'static Queue>, > > > } > > > > This specific example seems like it would be better served by the > > existing `Cow` type. > > We use `no_global_oom_handling`, which gates most `ToOwned` > implementations (e.g. `str` cannot implement `to_owned()` because it > cannot guarantee allocation success). > > So the Rust `Cow` is pretty much useless in the kernel. It's also implemented in `std`, which the kernel doesn't include. (Which is actually good for us, since we can't really use it.) Josh, how do you feel about adding a `TryToOwned` trait to `core::borrow`? This would be similar to the precedent of `TryFrom` in addition to `From` for the fallible case, and would be usable by the kernel.
On Sun, Dec 4, 2022 at 6:36 PM Wedson Almeida Filho <wedsonaf@gmail.com> wrote: > > It's also implemented in `std`, which the kernel doesn't include. > (Which is actually good for us, since we can't really use it.) We have it around in the kernel (the `std` one is a re-export), so one "could" replace the `Either` with `Cow` in the case of the commit message via ignoring the to-owned side of it (but I assume Josh didn't mean to suggest that). Anyway, it can be easily configured out from our `alloc`, so I will send the patch. Cheers, Miguel
On Sun, Dec 04, 2022 at 05:36:08PM +0000, Wedson Almeida Filho wrote: > On Sun, 4 Dec 2022 at 10:31, Gary Guo <gary@garyguo.net> wrote: > > > > On Fri, 2 Dec 2022 15:41:59 -0800 > > Josh Triplett <josh@joshtriplett.org> wrote: > > > > > On Fri, Dec 02, 2022 at 05:14:58PM +0100, ojeda@kernel.org wrote: > > > > From: Wedson Almeida Filho <wedsonaf@gmail.com> > > > > > > > > Introduce the new `types` module of the `kernel` crate with > > > > `Either` as its first type. > > > > > > > > `Either<L, R>` is a sum type that always holds either a value > > > > of type `L` (`Left` variant) or `R` (`Right` variant). > > > > > > > > For instance: > > > > > > > > struct Executor { > > > > queue: Either<BoxedQueue, &'static Queue>, > > > > } > > > > > > This specific example seems like it would be better served by the > > > existing `Cow` type. > > > > We use `no_global_oom_handling`, which gates most `ToOwned` > > implementations (e.g. `str` cannot implement `to_owned()` because it > > cannot guarantee allocation success). > > > > So the Rust `Cow` is pretty much useless in the kernel. > > It's also implemented in `std`, which the kernel doesn't include. > (Which is actually good for us, since we can't really use it.) > > Josh, how do you feel about adding a `TryToOwned` trait to > `core::borrow`? This would be similar to the precedent of `TryFrom` in > addition to `From` for the fallible case, and would be usable by the > kernel. I'd expect it to be in alloc rather than core (though I suppose it doesn't *fundamentally* depend on an allocator itself), but I'd be happy to see a fallible version proposed, yeah.
diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index a3abc110ff97..53040fa9e897 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -31,6 +31,7 @@ mod static_assert; #[doc(hidden)] pub mod std_vendor; pub mod str; +pub mod types; #[doc(hidden)] pub use bindings; diff --git a/rust/kernel/types.rs b/rust/kernel/types.rs new file mode 100644 index 000000000000..3b0c44769708 --- /dev/null +++ b/rust/kernel/types.rs @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Kernel types. + +/// A sum type that always holds either a value of type `L` or `R`. +pub enum Either<L, R> { + /// Constructs an instance of [`Either`] containing a value of type `L`. + Left(L), + + /// Constructs an instance of [`Either`] containing a value of type `R`. + Right(R), +}