Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp306279rdg; Thu, 12 Oct 2023 06:22:20 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHZ5eBF7piF9l6OE/p/iCKryuIOOnAHLyivnxnNlfCvkidLIXCpvqlSPcOnEasEZ/qNb2XF X-Received: by 2002:a17:903:2103:b0:1c9:e774:58d0 with SMTP id o3-20020a170903210300b001c9e77458d0mr490364ple.34.1697116940271; Thu, 12 Oct 2023 06:22:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697116940; cv=none; d=google.com; s=arc-20160816; b=MJdtUvq6cX7ThnQeztsIRVLEUErq6kRRbBih+iFNln3Vis+2TN2WzsfEbys9/znCxd 2eC3kA4vBOxnIJ+nAkVZzGly7YAv7bp5vFexjNIZSsOLI+ZgjYaO7akf3+VNPhSJ/cz8 dTKuQR0Ahy3oKwmSFxYz+Wb3gJ/dgD4RPdAP4WISrXiXiKw6YJ8RG5gj3cymj1QVPFcl nnNkNszTn8avuSSmZoRIQ7tQgXD6+UzfyPUVlSUInqMj6ekFZ5T0/3Q3c7qyVmpfWrp8 NiGjfaGEmnfoBLw96cLgFiYPDMHTERZo3qkgpZx+9mcT0e/aMfm9KMqmyiE/XdSU8Qch OE6A== 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=a9Ak0X7bSTujHRssN+a+Hftra+UlXwNVYvmyqQQCilw=; fh=V11RwCzO6FhrwwR400IeaCztZOiMPvinK8BR27/cP4s=; b=v0TFdd9HE3bQF29KZJ+yT2IaNMV7XubO9A5kWclndpdJf1Mufh1XOvzHLdYFBsOGfa zG4pMglK56FZljgUlAVWsh+9rJxwS2xv/t3ZGJhl1lvZcQTshAec4JClqbXbpMsj8niy e79KKBXW3o+c1Q9gtLaoxiqu3ilIGDvboNvku1eE23vOHJAZt/njfaI3oGJFl3hjvsJy M2ZkYHLnnSz6znJxA7pFAjA0KgGS8QOqANBfYfXOd7QLI73CbD3caVFIL6UnuWehXkBC oDhXJWKcLN/ITOfXPbfk4ojVVuBpxEUGKkVGX62TrcjLZifNkkiXNp7a0TpK8zmn8q5S d8Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CakwaJnK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id f4-20020a170902ab8400b001c78345f376si2098752plr.241.2023.10.12.06.22.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 06:22:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=CakwaJnK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 9A1098226AF2; Thu, 12 Oct 2023 06:22:17 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347153AbjJLNWE (ORCPT + 99 others); Thu, 12 Oct 2023 09:22:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343733AbjJLNWC (ORCPT ); Thu, 12 Oct 2023 09:22:02 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79B4594; Thu, 12 Oct 2023 06:21:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1697116916; x=1697376116; bh=a9Ak0X7bSTujHRssN+a+Hftra+UlXwNVYvmyqQQCilw=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=CakwaJnKAjcxT7b1G6jSKT+/BpZ3n4TnAJBO+wUhocU6wDlKI0nP6WhL3GDpYIuVE 3k+/Yb3acb18mnH+POGqFjYWjep/842QnWQkdVKtVjlEIvZNx6QofnjHqFN9j6Be96 DR9+/HiPdmnid2Fx0HaIcmtRo69k3Yft/IhUMoKmtFjrfi2t3jv+BuuVYL7e5T2YrQ QzgYq9iQfC+Yj69RHyPe0jMoPIMcR+Et7ovrd4/H+i53LxRniPGghs2zgQyyBGdRaI XtxPXUwaLpCc4pxaSGm+6QWomk58RgZKY+Te7tgXc8Sqs5ZQYbeqEkamajMZ/9F+8u qJ19tjsuo75bA== Date: Thu, 12 Oct 2023 13:21:46 +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] rust: macros: improve `#[vtable]` documentation Message-ID: <20231012132131.300014-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 howler.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 (howler.vger.email [0.0.0.0]); Thu, 12 Oct 2023 06:22:17 -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 --- 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..dab9a1080b82 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. +/// +/// If you want to make a function optional, you must provide a default im= plementation. But this +/// default implementation will never be executed, since these functions a= re 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 t= o replicate the default +/// behavior in Rust, you should use the following code: +/// +/// ```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: b2516f7af9d238ebc391bdbdae01ac9528f1109e --=20 2.41.0