Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp504045rdb; Thu, 19 Oct 2023 10:16:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFbRoKL6J0GpuJCHqsgNvTgTM5bU14M0h8M905MWkbDUdsnnpuKcthj56a5oj2NEsoWXg3t X-Received: by 2002:a17:902:f550:b0:1c5:ff18:98af with SMTP id h16-20020a170902f55000b001c5ff1898afmr3480883plf.4.1697735790138; Thu, 19 Oct 2023 10:16:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697735790; cv=none; d=google.com; s=arc-20160816; b=rFilAfaIMj++YY2vySb7qJBXcM0xWb3DHxWm9dtPTx/FGlm3U+T6zJT4yTCdII4MK8 CWtrNLyUgZ90RS+JVwArzgDjqK3PNYc3WbZ0i/fARJl5pCQwvuVeC4s1Ogoj7etTWS5O TuRvHQAZVsjNHY24vellMxK1RJsIr0cm8WHk04JAtNrMn1ztYJNTm18+dNwXDxK/rteX jSxXzIcV2Q0gnxbWJDpd+/rR4U3Z4JBTLhRhs20T+A//nKAOeLR7s0t25V5EE9T7tLwH ZCHvDeJrVeDmNtWy5tMVaJFvGVuKHd6st8w69QZ93nExPUMJI1ONVyS4P1TddFJlD/D0 qN7w== 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 :feedback-id:message-id:subject:cc:from:to:date:dkim-signature; bh=w1TaW2ie2JiPlJ9irZEgwnd/XHXqHoBuv2KG6JL9RIk=; fh=V11RwCzO6FhrwwR400IeaCztZOiMPvinK8BR27/cP4s=; b=dkA03PQ6wiZ2KHpwcmeBXrc7sFCLoa0IxLNsnaRWTkkzwz0+8OIFEwAPEhEoT/XG44 1n8AzRus99ZBQAvR4tWJSncE4hLwajtue3RQ2PpC3fIAlrzE1wNhRqQ7VNCfTmKAS8G4 LfVCEPeL9wVociT+LbX6kC1Fp4Q4cAa+s352N8s8nyGDdMznhjT4HX0dQoIzcZWYHM/i bGgzXRN/gRN1znEz5tUleU76pqFaWwwzFIyfEBXhdglPkR2Fa7MBDdasnSH9/5FKAcBC bBeOYitHzed23R8IN1YwtIWKmZMXUkyDlxSb6ASqhm4B3wSPj0zbE4hxCEQpgvlnaiBU Lo2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=DxYTL6RN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id n16-20020a170902969000b001c9b5a96d15si2484621plp.13.2023.10.19.10.16.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 10:16:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=DxYTL6RN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 62B2681B454B; Thu, 19 Oct 2023 10:16:27 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233238AbjJSRQU (ORCPT + 99 others); Thu, 19 Oct 2023 13:16:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233017AbjJSRQS (ORCPT ); Thu, 19 Oct 2023 13:16:18 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F030CCF; Thu, 19 Oct 2023 10:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1697735770; x=1697994970; bh=w1TaW2ie2JiPlJ9irZEgwnd/XHXqHoBuv2KG6JL9RIk=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=DxYTL6RNUPIK+Pkqg7PZhn5B1I/ZpvkB4MkGcHjo25tMOxKN+Qs9lWrTAaCriWePB Kpl6+tLqx6CHEUoUBAfLi/wPDhHdAYLRIPRY5hz0kDtUbryQlTbro3BTazNY7xKg1h 2etDSAcayiLP95yAyIUJxaZuFM0iGkPwQuF+teaJ1Q32iL1tL3ev+otHyLudpYkvu2 t84xY+bZidkLwGS3MZ4HFzwe0fRtNRU0x4RYO1SBLG1PZbw5IEx+PQcACBDTiEb8zi 0ZXXWc2ICPgYQONrZ9mbJvfunyZvWgl0/KWTR4XCaAgRFkdyycNC57sWL4/dDsyE/k u3IkKr8MkSOXw== Date: Thu, 19 Oct 2023 17:15:53 +0000 To: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor From: Benno Lossin Cc: Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] rust: macros: improve `#[vtable]` documentation Message-ID: <20231019171540.259173-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 10:16:27 -0700 (PDT) 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) =3D> 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 =3D + "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 an= d -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associated c= onstant -/// will be generated for each method in the trait, indicating if the impl= ementor -/// has overridden a method. +/// This attribute closes that gap. A trait can be annotated with the `#[v= table]` attribute. +/// Implementers of the trait will then also have to annotate the trait wi= th `#[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 exclu= sively called from +/// callbacks from the C side. This is because the vtable will have a `NUL= L` entry and the C side +/// will execute the default behavior. Since it is not maintainable to rep= licate 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 you might need to import [`kernel::error::VTABLE_DEFAULT_ERR= OR`]. /// -/// 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.VTAB= LE_DEFAULT_ERROR.html #[proc_macro_attribute] pub fn vtable(attr: TokenStream, ts: TokenStream) -> TokenStream { vtable::vtable(attr, ts) base-commit: a7135d10754760f0c038497b44c2c2f2b0fb5651 --=20 2.41.0