Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1815874rda; Tue, 24 Oct 2023 04:24:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHsOBHzd5mVy3ZFJTNX+NtvQZMXUsMQH+bBWPcxCK3draLe57eflZrudusXMCTe3rKsBg8f X-Received: by 2002:a05:6a20:8e0f:b0:161:aef5:6395 with SMTP id y15-20020a056a208e0f00b00161aef56395mr3128851pzj.24.1698146688455; Tue, 24 Oct 2023 04:24:48 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698146688; cv=pass; d=google.com; s=arc-20160816; b=Md+/AkObbRJfFmOxzPn2MNUON0s5DWZnTYrw/zCKWWLBgr94s67jIq4lMmg53neC3U KlxXl3svBzcD1LNbdLFU8ewM0L2GXkUxAcaodz7HaneP/09KKGhjgKOYwkAhKYZjMfkd paFsnXtOu+IiPLVDTD/CsbkHfs3I1+GbIK3pT4yrNPdY+jbJhAhLGJjgme/qJJ2SmPBq MoNDaoWkUf2tQW3JindkH7OADq4LkrhSj5DqIGQfNdYI40TTNzopoqvBTkDj8SscOP+y d86tdMa7pOEv2qT8PpZKhKrgC63Beo4iyKketADdNN1ANMfwDPgZRRS1k5sO4JrdUv2X g1rg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=JAYQfY9DfQeZ8+GcppYXBrRnxAoSBIwx+Tv5+6g1exw=; fh=6Bzh4Uey/qjeRRXGmdFRnczmgve6eYJO8qlxg+sXrrQ=; b=wkT+Y7X38tIMMKCm2P+DVCT2tIueahO1d0pBoNKVPrgZrqIYXUdHBF/A60J/7AIflF E1pqAO/BEBytv7b2lgEtvLAkIgU6L0FRpnJ2gThW0kpdAGMNVLLUBB58RZBLw7qf0hWY eHWvaJTERFGAtUdvQMTtnZoTZPm5NQntan2qrNPGB4WkmrwWLoLu2p7SY83yr2x12PUh LakxXDmRijpPZ+nmVE1L43e+N6P46N6BjGQ+nMOKZEoHByclPPg0aWwEwIhgxKBQG4Zp DQOc8Vog0dpQBgaluDbJR+kwPaVPls0x8KPvtzO5Z/96eDEsis4amrYObQi+7Dc5EzPn 693g== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=pNm7UBDP; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id f17-20020a056a001ad100b0068a54522bb0si8536417pfv.144.2023.10.24.04.24.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 04:24:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@garyguo.net header.s=selector1 header.b=pNm7UBDP; arc=pass (i=1 spf=pass spfdomain=garyguo.net dkim=pass dkdomain=garyguo.net dmarc=pass fromdomain=garyguo.net); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E22FA8034611; Tue, 24 Oct 2023 04:24:45 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230343AbjJXLYg (ORCPT + 99 others); Tue, 24 Oct 2023 07:24:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229972AbjJXLYf (ORCPT ); Tue, 24 Oct 2023 07:24:35 -0400 Received: from GBR01-CWX-obe.outbound.protection.outlook.com (mail-cwxgbr01on2107.outbound.protection.outlook.com [40.107.121.107]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72B6CFE; Tue, 24 Oct 2023 04:24:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SSFlsNgXW24MJ9h4eXD97sM5v9fCwvUgod8lLGu837V11MGVUbIDDetqdcpa/MPDSxpt3InNWR1AG+wZA3zGwJA/VL6yNH3eNGulzxAm2hHv/TUUQCXqniVsR0XQU7vM1CR/Crfp9/OAbO5JafVZ2EdLwJaoXL7HVTGvSOtI0mRHMtKJbDj2LLtkl4JY/ndcWp2ed/p/dD4o656CBm1tZavZGYl/Oz5ta0ipBp/OA40FFWHrwb1gEnH3JeV5mIQhSabnHJA33DeBlKoeulysy99nGv5vJGstKW4bV/wdvBNWgCxgXxj18OGWRu4/Ww/0GnfpeGOu6+xnvyhqkmFTMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JAYQfY9DfQeZ8+GcppYXBrRnxAoSBIwx+Tv5+6g1exw=; b=HDYG8XkruusyVbFv3GEclEnjne0zfr45KgQjHuATJ/lYPZfmC3fXIO/BeN0zvzh6y7j1mw8KHudzIyL1InFuzBmLEILTta9HkFAWTSisqiDazR2eBhq+WPBmubx+5lbPeNU+YWH2vpKWf7LuV0ofN+KKizb2T82D8KurpdHZa8KKl1VjO7CDL2qNJ1bL5bNzWoKdgtGiB1KPpDSyepNN808HmbI3lS6ABuHD0CwEDvE/hzsbOSbSeY6Mc9Ll889HlIf4xSUlTK7zRvOKHv22kCI9rHsW+v7n05ht7ia3AJYQkr1j8dE9JVUR/yHsKO+7WSAZzK+Ha5gLajnX0nnNaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=garyguo.net; dmarc=pass action=none header.from=garyguo.net; dkim=pass header.d=garyguo.net; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garyguo.net; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JAYQfY9DfQeZ8+GcppYXBrRnxAoSBIwx+Tv5+6g1exw=; b=pNm7UBDP22PVGUxhdF1z3zl8mX1wTjT2dqqYwOkeBjq+7f5uuw3eGcLHqNXWNTX0R5JDP0FO8DhBt90YrQSPD2EpHqUNR+3zQYp6Y9G52jKXc02XAAyYsIhnFpo8ZXSTjeUfzx7IU6CUER8+Af5gDTsHogDqQ289xPmSH47Wt2w= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=garyguo.net; Received: from CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:15f::14) by LO2P265MB5856.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:268::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6907.26; Tue, 24 Oct 2023 11:24:26 +0000 Received: from CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM ([fe80::6af2:36f2:4aa:6e43]) by CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM ([fe80::6af2:36f2:4aa:6e43%6]) with mapi id 15.20.6907.032; Tue, 24 Oct 2023 11:24:26 +0000 Date: Tue, 24 Oct 2023 12:24:23 +0100 From: Gary Guo To: Benno Lossin Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , =?UTF-8?B?QmrDtnJu?= Roy Baron , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] rust: macros: improve `#[vtable]` documentation Message-ID: <20231024122423.383ea1bb@eugeo> In-Reply-To: <20231019171540.259173-1-benno.lossin@proton.me> References: <20231019171540.259173-1-benno.lossin@proton.me> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.37; x86_64-pc-linux-gnu) Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-ClientProxiedBy: LO4P265CA0248.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:350::8) To CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM (2603:10a6:400:15f::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CWLP265MB5186:EE_|LO2P265MB5856:EE_ X-MS-Office365-Filtering-Correlation-Id: 3dfa16f7-801d-4c23-8906-08dbd483c7b5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: bazps4AwFhuUCDJKizZXT4XtxXjuCcmyJbfUEY6x83F0dWtztGFUHeiASkkGW2lIpCh1QW769f35/DfanlcYvuTo0fe/CzpwXFkGj3cS+jkPCubf6XXXEvAzOdxSLLBLMR0bbvlUNFpSccxVybYT0h07cwo/fdbw56WWjWnhZ9WwTXeMBFrygkRixBLPbPk77t13vh88A+IwONS5nxW/PWGeEgSzGn8tyFHGl9z95pSCMaZlqcB0HZhnEVgPUrv5V8kpVc4UHqEXPhVZRvjKzR/gLZMPWqGDdzbgBTU6pUI9vxEAsesHdXjwwaoyRyV+DCt5CIDRTEgRThFyhTZDm2aALIc+ZAjvW81QTGkupZQlFszfnqDTVb+yg0J2PHM3oL5oChKNbZIFkiulVXv9kDSmp9c09xKRZ7DrXs6w6tnhpUW/beFaEm/oSqionUAmxiyWDfjUO7Krh6F2BFMLrOVkNiEdZwBzfV/WnCXrCQHSuX7kPTSeoWOPODIIMxFRFRdTHeWF7K4pscjgeM+QVa6rlZ69xlX2K2U0LubrZZu2z9zPnqp8SOwSz0LuSmoW X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230031)(7916004)(366004)(136003)(346002)(396003)(39830400003)(376002)(230922051799003)(64100799003)(1800799009)(186009)(451199024)(2906002)(7416002)(54906003)(316002)(6916009)(6512007)(86362001)(66946007)(9686003)(66556008)(66476007)(38100700002)(6506007)(33716001)(6666004)(6486002)(478600001)(83380400001)(4326008)(1076003)(26005)(41300700001)(5660300002)(8676002)(8936002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xfI0cZQHHSzsuVgW3dFACN2KL0nMs8cxlxUi1gvqkUq41m9+Wi9KV/hbfDc4?= =?us-ascii?Q?UBKhLHFQCRtM9FLwhf/84tFrwBL/VLpIvpGydHCnLerNvkm4c1T4EvERd7I9?= =?us-ascii?Q?03r44JAvNXvpRH8gezH0a0M89nObTGDhz3KxfCvmb1/8kzDJgk/S+XYXwUaS?= =?us-ascii?Q?m418fz0ftjIbghWKVk/8nQhuHXndXpzGdVYwK0+G8/zFnro34Cku4Jlo/172?= =?us-ascii?Q?AJdNKLmi82YQ2owgWFqRaLCp3dbc0x7rYCsslhAqKEmTkOSJJYB+KrqiGxd8?= =?us-ascii?Q?HxU1gFtXZxyinHz3KXb2vW0SvzowPyOFzcON9ku9IhzV8s6D1gMuIJgbNeag?= =?us-ascii?Q?UYDqWXsNJiADhpk3/hcHt1Or1kWuJb4C+KTX64qRIcXBdSQZLO8MIZxpYlkj?= =?us-ascii?Q?cxlI4NRyk2cWn9HSYGLw5uZq29/vyrh4cFsx0e492qdLqVcEmKO3P/+h8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Pk6oBvqrGt9ufHqwOt5LYhVP6TQo8T+ngowDVORGLV8wZX8A0E?= =?us-ascii?Q?kBAbOhzUWmXUWfsUE1scR7EoLZnYU4gOjaaz0oz4moIJZ9gX5HpzxmCxErYi?= =?us-ascii?Q?V+0NXzGb7cu8GcLRdPRNo2eI1XlTejdsmxYFSa8mBlIdK07Pauv4DHGgdVoz?= =?us-ascii?Q?SOhbFPjSjS4dWCA68/5SWYrEl/K4glQBwuvo4cFTx/rDIYRxEm8ESaSu94jH?= =?us-ascii?Q?+1YQSPJ6tvcz4bVebORjwJVpIurES3sCMM0fMTBq?= X-OriginatorOrg: garyguo.net X-MS-Exchange-CrossTenant-Network-Message-Id: 3dfa16f7-801d-4c23-8906-08dbd483c7b5 X-MS-Exchange-CrossTenant-AuthSource: CWLP265MB5186.GBRP265.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Oct 2023 11:24:26.1079 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: bbc898ad-b10f-4e10-8552-d9377b823d45 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: GN5IsK7zRWTyC5+JcBQVJdr7EytR67QVdhtBKUsfrZIwrIiKmMoWV/IPTtCimMmBnMN4HxS2WQskcQ3P1dV7vA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: LO2P265MB5856 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 24 Oct 2023 04:24:46 -0700 (PDT) On Thu, 19 Oct 2023 17:15:53 +0000 Benno Lossin wrote: > Traits marked with `#[vtable]` need to provide default implementations > for optional functions. The C side represents these with `NULL` in the > vtable, so the default functions are never actually called. We do not > want to replicate the default behavior from C in Rust, because that is > not maintainable. Therefore we should use `build_error` in those default > implementations. The error message for that is provided at > `kernel::error::VTABLE_DEFAULT_ERROR`. > > Signed-off-by: Benno Lossin > --- > v1 -> v2: > - removed imperative mode in the paragraph describing optional > functions. > > rust/kernel/error.rs | 4 ++++ > rust/macros/lib.rs | 32 ++++++++++++++++++++++++-------- > 2 files changed, 28 insertions(+), 8 deletions(-) > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs > index 05fcab6abfe6..1373cde025ef 100644 > --- a/rust/kernel/error.rs > +++ b/rust/kernel/error.rs > @@ -335,3 +335,7 @@ pub(crate) fn from_result(f: F) -> T > Err(e) => T::from(e.to_errno() as i16), > } > } > + > +/// Error message for calling a default function of a [`#[vtable]`](macros::vtable) trait. > +pub const VTABLE_DEFAULT_ERROR: &str = > + "This function must not be called, see the #[vtable] documentation."; > diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs > index c42105c2ff96..daf1ef8baa62 100644 > --- a/rust/macros/lib.rs > +++ b/rust/macros/lib.rs > @@ -87,27 +87,41 @@ pub fn module(ts: TokenStream) -> TokenStream { > /// implementation could just return `Error::EINVAL`); Linux typically use C > /// `NULL` pointers to represent these functions. > /// > -/// This attribute is intended to close the gap. Traits can be declared and > -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associated constant > -/// will be generated for each method in the trait, indicating if the implementor > -/// has overridden a method. > +/// This attribute closes that gap. A trait can be annotated with the `#[vtable]` attribute. > +/// Implementers of the trait will then also have to annotate the trait with `#[vtable]`. This > +/// attribute generates a `HAS_*` associated constant bool for each method in the trait that is set > +/// to true if the implementer has overridden the associated method. > +/// > +/// For a function to be optional, it must have a default implementation. But this default > +/// implementation will never be executed, since these functions are exclusively called from > +/// callbacks from the C side. This is because the vtable will have a `NULL` entry and the C side > +/// will execute the default behavior. Since it is not maintainable to replicate the default > +/// behavior in Rust, the default implementation should be: > +/// > +/// ```compile_fail > +/// # use kernel::error::VTABLE_DEFAULT_ERROR; > +/// kernel::build_error(VTABLE_DEFAULT_ERROR) Note that `build_error` function is considered impl detail and is hidden. This should use the macro version instead: kernel::build_error!(VTABLE_DEFAULT_ERROR) Actually, the string here provides little use other than documentation, since the string provided to build_error is only visible in const eval, so this you might just omit that and write kernel::build_error!() > +/// ``` > +/// > +/// note that you might need to import [`kernel::error::VTABLE_DEFAULT_ERROR`]. > /// > -/// This attribute is not needed if all methods are required. > +/// This macro should not be used when all function are required. > /// > /// # Examples > /// > /// ```ignore > +/// # use kernel::error::VTABLE_DEFAULT_ERROR; > /// use kernel::prelude::*; > /// > /// // Declares a `#[vtable]` trait > /// #[vtable] > -/// pub trait Operations: Send + Sync + Sized { > +/// pub trait Operations { > /// fn foo(&self) -> Result<()> { > -/// Err(EINVAL) > +/// kernel::build_error(VTABLE_DEFAULT_ERROR) > /// } > /// > /// fn bar(&self) -> Result<()> { > -/// Err(EINVAL) > +/// kernel::build_error(VTABLE_DEFAULT_ERROR) > /// } > /// } > /// > @@ -125,6 +139,8 @@ pub fn module(ts: TokenStream) -> TokenStream { > /// assert_eq!(::HAS_FOO, true); > /// assert_eq!(::HAS_BAR, false); > /// ``` > +/// > +/// [`kernel::error::VTABLE_DEFAULT_ERROR`]: ../kernel/error/constant.VTABLE_DEFAULT_ERROR.html > #[proc_macro_attribute] > pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream { > vtable::vtable(attr, ts) > > base-commit: a7135d10754760f0c038497b44c2c2f2b0fb5651