Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp322561rdg; Thu, 12 Oct 2023 06:48:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE19mkIY005Xa52EGGW3E3peTWNuXRf00H0Tbq/elS7ikkB/AIXew0ake9kEI+rpdlBAVIt X-Received: by 2002:a17:902:c40a:b0:1c3:a4f2:7c92 with SMTP id k10-20020a170902c40a00b001c3a4f27c92mr25216809plk.65.1697118536517; Thu, 12 Oct 2023 06:48:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697118536; cv=none; d=google.com; s=arc-20160816; b=cLKHIPBLZgjUjemy6itQnVvkks21t3x09tB9IXHx1n8gPwfedTS3Z8SOyZgJ8DR53E io2jaKEjy2TzDljTojiBE+AWUrcogltbiBen6xiCDtebbLk4BgH0uIoXlQX2aJ+cbh+n lGtXxmMIL6FXseixYVn8n3k9lrlGPMNBK9X+QOrbeZFbBqLfeSG4Th5tLkEfcg5aHj4a 23tK2obOY3XF8GYZti1gHzPVe3rwZAmByHheR0AwMWUBC+L3RHXazbxYc00hIx2obFdV krvBZA5vTUGaAF/3S5Cy4NPl3jh6BCi6S2yWLE3Vo8lhLxwPfoEpQdFXNz87jdRF6cls SigA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=jbsW+VEW2ckEOL33neXtq1kKRPj1Kbmo2QFoSTgB9hs=; fh=xLRFJbKGoq3WOJrIBuQ8xNP1dOkVcctoOgeG1YU388w=; b=EqnrHGoTFf9/I7sr71YqyVVm2KjVWDf9mj3otO6bLTVenhYJXqBnmIwiWYpCGzNP/v ZttO64omKEYg5mrIMQyNoVxI1KTgulgc260WJo6wggcMMyS0EKOF6e0Dwvu3/clS8BRJ FBvT9cP/JsxPuScYoupZk3KE4351tl92t5EsqFTb+r4P/DW+PyTx4Ey5PH12q9VOaEEw Tvqo0CEX/4KZCRYZWrhZg9xCvmSP/E/46K4S2u6snkzu2mgIqvKrQqTFkZUHr00e0nwa hsq0nRzLVfOxy3qzlvOaAZ+lhXC87XCBU/RRbNgXPe/r09VJLhChgyzxqKVnuddN+I+n saow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aB5ZrvpM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id io13-20020a17090312cd00b001bf5c12ea06si2077559plb.404.2023.10.12.06.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 06:48:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=aB5ZrvpM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id E36028229F23; Thu, 12 Oct 2023 06:48:52 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1378980AbjJLNsj (ORCPT + 99 others); Thu, 12 Oct 2023 09:48:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35664 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347289AbjJLNsi (ORCPT ); Thu, 12 Oct 2023 09:48:38 -0400 Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D537BA; Thu, 12 Oct 2023 06:48:36 -0700 (PDT) Received: by mail-yb1-xb2f.google.com with SMTP id 3f1490d57ef6-d9ad90e1038so458078276.3; Thu, 12 Oct 2023 06:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697118515; x=1697723315; darn=vger.kernel.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=jbsW+VEW2ckEOL33neXtq1kKRPj1Kbmo2QFoSTgB9hs=; b=aB5ZrvpMSLrsmFWEv/z+RnlfOLEUuHjtO94fLazqlZdv3vK6zH72Qwj6mUDf0xe2A/ i0DrsYCRDeu0HJuQxq74/yxfDLf1GLOBkgAIRC5xcY8ySQLUz0E2nD+UNLyqoJ+9ajnB 0fnAlMyOGvAeQzRei1lU8KnI3a7HZz1aNsVPdS8TEZ351OgOK0ScGf9TLzUPKX/ocAWX XffEkOMPhwwvXlzmEQJRDXGtSN9D6YR4zXH+WU/UZM9eLaJpjZc2StuhUF3uCRt56lcF +nXIATStpfy8f4B9aa+rUn6QXy6yGDkEqqKiyFs89NzGNcQrwlPegLIYQVBK7whyp3+f ObFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697118515; x=1697723315; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=jbsW+VEW2ckEOL33neXtq1kKRPj1Kbmo2QFoSTgB9hs=; b=cBdqovYQdQCcnF3YTIIKZcX+LFVofYf23P7/U1pCTjDFIyKesihSZEBHojl+VQUitN 0djE7CzgvPyqde+JGmynqblkbT/3ZuJkLVfGW2LkDcqfSeO+WvUI246T5EoX++beLMDS yXoe02CV6ndjL4bem7ma+XiCnApDqoWp6E6Ltf/iUyAKrKO07tnmV/W5qVA/tquHXszE sgSZkt+yPZaR1T93Bjex+O/uU94aaURcX2bcMDI7KkXLYOTbAsyirWwR4FmFc4uJVtUi WIeLjTun6a4PCLuHvFl8eM1VoUgZGQgtbgx7f9US5t8oKCMvh+789fkDRvZxzKl5VUIr QkiA== X-Gm-Message-State: AOJu0YyIHde2pYz+2IqMqy0gJbqUpkDbbPwCkNUq9LgjE+5CaWkJzidY 5MydGeYOA4+RwIyzr27/7tOj8W63CIrha/zt+Rw= X-Received: by 2002:a5b:c3:0:b0:d62:6514:45b7 with SMTP id d3-20020a5b00c3000000b00d62651445b7mr22289127ybp.37.1697118515444; Thu, 12 Oct 2023 06:48:35 -0700 (PDT) MIME-Version: 1.0 References: <20231012132131.300014-1-benno.lossin@proton.me> In-Reply-To: <20231012132131.300014-1-benno.lossin@proton.me> From: Wedson Almeida Filho Date: Thu, 12 Oct 2023 10:48:24 -0300 Message-ID: Subject: Re: [PATCH] rust: macros: improve `#[vtable]` documentation To: Benno Lossin Cc: Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Andreas Hindborg , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 groat.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 (groat.vger.email [0.0.0.0]); Thu, 12 Oct 2023 06:48:53 -0700 (PDT) On Thu, 12 Oct 2023 at 10:22, 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 Reviewed-By: Wedson Almeida Filho > --- > 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..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 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. > +/// > +/// If you want to make a function optional, you must provide a default implementation. But this We should standardise how we write our documentation. In the `rust` branch, we avoided using the imperative mood like you have here; the rationale was that the documentation was describing how things are/work, so we shouldn't be giving orders to readers (they may be authors of traits, implementers of some vtable trait, or neither, just someone learning about things, etc.). In the paragraph above, you'll find an example: "Implementers of the trait will then also have to...". For the specific case above, I'd suggest: 'For a function to be optional, it must have a default implementation.', or using the passive voice, 'To make a function optional, a default implementation must be provided'. > +/// 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, 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_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: b2516f7af9d238ebc391bdbdae01ac9528f1109e > -- > 2.41.0 > >