Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp246656rda; Sat, 21 Oct 2023 06:19:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJPYB1dKX54Djh0T36IdTTElnXYhJTRaj1mMN8x/MqYUsiYB+EnBjzTckQ5+yEriZigi2i X-Received: by 2002:a05:6a20:8f13:b0:16b:f6fe:33fe with SMTP id b19-20020a056a208f1300b0016bf6fe33femr4541137pzk.1.1697894379043; Sat, 21 Oct 2023 06:19:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697894379; cv=none; d=google.com; s=arc-20160816; b=t3UWR+yhaPgcqZeOiFMA22kA4z92AtYCoMgGoC+24IL6pc+DqCtasWfE2VuAhBWxgA Fl4V48ZV32tl8uoVj/aqVCuSxa5VzxrckYCPTJCF0vXoa1APNaIq78MdEeIHqmlI01BA 2Vq4gVahsGrgwIVRBzCKFvglPg5jy/Fm0icQKbTO3yqdRyq/STunV/xT05wsLqr32d6p o44HOnC1GIU+K9bVVvijajDftlOYbT1O6YGZCycBMe9TRvumoshHLap6DdcYvI3vXMwa fgN5taUvBW9G6Y2OF4CzTt6O6NcBU8bdZbsHCj6oJJ7ZKoovtNRkXTedUA4hUAu+hLXJ rsMg== 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:references:in-reply-to:message-id:subject:cc:from:to :date:dkim-signature; bh=sCSYxlsGEw/QK9x9M3aySq+pUCU4iIWD6t3o6q6q0a0=; fh=w/GYDAm1OSPm6jnmvGCoSM1r/XWzm3r2/r5Mmzdxurk=; b=ynpoI+6Us/Ec8HJzQVjXCqpPtNc0gZc5YA56DMYWQksmRcxeGwm2OA2XnB4rUKr+Kv 88Mgl9qleJCSazWxd9/vzO/0yGKIc8MAib73+Uh17+T74St0ixVaIc56lJX+JjMjf7yl L1lRPAms9mo/DOXiejbqvZ4BspjMHx3WDl5IA+BLzk5jf8z96HQnfcoMl7fFUwme9Lp2 xJJbeIYFmAwzkp/BOgPKGrvhFkJ9zxLNsfVRQ9Qfk3INoxbAp2oKs9jhqhHV+MFk0Gqr 3tNnd44gZCapPWe7FnEi3LzbVK4X9fBdJr4Jb3jIbJkb/EGDCoMCnZgRV9B6dSQGaTVJ jV8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=N6pMv6qs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id f1-20020a655501000000b005b5c924a6c6si3567763pgr.744.2023.10.21.06.19.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 21 Oct 2023 06:19:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=N6pMv6qs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 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 morse.vger.email (Postfix) with ESMTP id 265928028049; Sat, 21 Oct 2023 06:19:34 -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 S231436AbjJUNRN (ORCPT + 99 others); Sat, 21 Oct 2023 09:17:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231162AbjJUNRM (ORCPT ); Sat, 21 Oct 2023 09:17:12 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2A657D63 for ; Sat, 21 Oct 2023 06:17:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1697894224; x=1698153424; bh=sCSYxlsGEw/QK9x9M3aySq+pUCU4iIWD6t3o6q6q0a0=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=N6pMv6qsPg8lw9tQgNb5buHejJnDSOhFAgaZFFnRmDs3UpLYfqUl55aRSU4npHgj5 dJMbU6qR9mB/JYlOt5Bd0E2UwB/xDty9IUQZXPmXOB9kTqEyPK5xOKG99nFaqiUckR HFqQvdJrVK7oddIYT9AH7bL7O8rSTJ9opFkjayfDRJ0jSGiDdxafy8KVbbSaERCBw4 fETFnPhPVTVPBBB061Cf9ly60ZQxzzV/ztuRqhMf4ZkqJEvqDbHG+2QEDZO/lgQrvq 1zbnLFS0FPUMVWb7qOyJU/8korZ+8TceYE0jQ40GuETYc1MHP6Go/R2IIwbwAFKXSm kbgfeA9dKlgew== Date: Sat, 21 Oct 2023 13:16:54 +0000 To: "Andreas Hindborg (Samsung)" From: Benno Lossin Cc: Miguel Ojeda , Wedson Almeida Filho , Alex Gaynor , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] rust: macros: improve `#[vtable]` documentation Message-ID: In-Reply-To: <87fs25irel.fsf@metaspace.dk> References: <20231019171540.259173-1-benno.lossin@proton.me> <87fs25irel.fsf@metaspace.dk> 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 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]); Sat, 21 Oct 2023 06:19:34 -0700 (PDT) On 20.10.23 11:06, Andreas Hindborg (Samsung) wrote: > Benno Lossin writes: >> +/// Error message for calling a default function of a [`#[vtable]`](mac= ros::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= and >> -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associate= d constant >> -/// will be generated for each method in the trait, indicating if the i= mplementor >> -/// 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 met= hod 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 implementatio= n. But this default >> +/// implementation will never be executed, since these functions are ex= clusively 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: >=20 > How about this?: >=20 > For a Rust trait method to be optional, it must have a default > implementation. For a trait marked with `#[vtable]`, the default > implementation will not be executed, as the only way the trait methods > should be called is through function pointers installed in C side > vtables. When a trait implementation marked with `#[vtable]` is missing > a method, a `NULL` pointer will be installed in the corresponding C side > vtable, and thus the Rust default implementation can not be called. The > default implementation should be: >=20 > Not sure if it is more clear =F0=9F=A4=B7 I think it misses the following important point: why is it not possible to just replicate the default behavior? What do you think of this?: For a trait method to be optional, it must have a default implementation. This is also the case for traits annotated with `#[vtable]`, but in this case the default implementation will never be executed. The reason for this is that the functions will be called through function pointers installed in C side vtables. When an optional method is not implemented on a `#[vtable]` trait, a NULL entry is installed in the vtable. Thus the default implementation is never called. Since these traits are not designed to be used on the Rust side, it should not be possible to call the default implementation. It is not possible to replicate the default behavior from C in Rust, since that is not maintainable. The default implementaiton should therefore call `kernel::build_error`, thus preventing calls to this function at compile time: --=20 Cheers, Benno