Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1368192rda; Mon, 23 Oct 2023 10:19:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHtiWiuoC3A7Y6RLh70zUgcH2dcUZDRkeAKGoB4rHP5kSluqdVrLIbCtrqPvr0HR85alIXj X-Received: by 2002:a17:903:2447:b0:1b9:de75:d5bb with SMTP id l7-20020a170903244700b001b9de75d5bbmr11883629pls.7.1698081573611; Mon, 23 Oct 2023 10:19:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698081573; cv=none; d=google.com; s=arc-20160816; b=NyNVt/cZi2lqehVyNGbbkgiAdaWPTgQLkomHCkMpNURXVMPwivitrGYJo5mbdouEF1 QOeJ7OMbL6EBLCLfVwsVYTG46a3vTkQ5/vgEoxQa/tqA13QMxJSIV8wruMyKFIKNpFie UDPnvWm3aKaJeAGmyAXZXiuUQAPSwy7BWxof9xQkoElHfsNILRYhkwpMd28vkGbtQG14 cYKbf+1NFS25maNmxFBeqPZ71v2KyS8Nr5VUi8uakNqP+2FVNYMDSQjCwaDYvJ+t+XkE IpZ8b0BJ/eYL5mvWi2aBfArWvBSDzpPIQ8oxySF6UJ6ZOGYJoAY3/snqCSMXNaFztiOH ZrwQ== 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=o/kXFPMyJifrFRkXdqNgBwO5a87jm2R3z9kVsG8BBZI=; fh=w/GYDAm1OSPm6jnmvGCoSM1r/XWzm3r2/r5Mmzdxurk=; b=OZYqMflKUitXE8UICXYPgkOvqbdFT+3dIr+gEM0XaktNAN/g/ZByMBVInZzGGHnHpC Mj2YZN4hHDqO3oLTjN8TzlPqPpmrUNZMwFN9mtUHTiCJxYI2KkegYuFxUjZPzuGQ6M6B qx/eEeXf5RWhNeHh0AvjetFoSq8PYrJOyypbnSRO8Ft96T3BVm42mntwpw1uCliejxRm oLaK84VX0Kg5NoQbtTCchwfRNKB5hQhRsmeJnpL1rBRySlK/Pokw8Uc9qTr2CqOTxjYE OKMUIPucOyHK808whdTHdo+eGBzUCsgKIPjnZ4jI3Lat5Sm/2CN+iHcqFVl0bXRKE1l7 0W/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Lvy6KPbJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id m15-20020a170902db0f00b001b8a4954be1si6958111plx.595.2023.10.23.10.19.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 10:19:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Lvy6KPbJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id ACCC6804E444; Mon, 23 Oct 2023 10:19:32 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230515AbjJWRTa (ORCPT + 99 others); Mon, 23 Oct 2023 13:19:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229594AbjJWRT3 (ORCPT ); Mon, 23 Oct 2023 13:19:29 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BD56EE; Mon, 23 Oct 2023 10:19:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1698081562; x=1698340762; bh=o/kXFPMyJifrFRkXdqNgBwO5a87jm2R3z9kVsG8BBZI=; 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=Lvy6KPbJhQll4AotxXYUAz1N/fFLkHQCcD994EtQU6QGK0N9SSOnHNSg1JGG7npF1 nX+yM74MxptOB06C0KbNcGtpJ4MSXx1mBPh+Oh2v96dULoSvRabzNEGSIMf8LgoQJe zcDjhNTHqIkc+xPRkTZj5JsRADKCCtKSycz6vruUKz2dcBblYPMeuAeJnIPCbSTJTw wmxyV2RCluL+G0zJbT6jy5BfIsmgUPC6wcUPvm8pcJ4xEVCFj0/muUZUoDo4AqG8kW WjiYXk9k9IRtLQyu4H1qCitBXKWWdgVRgfrOojjn2u4SPkSStA/3SDNpYnt0dSvnpD vX4JG9ZW2FhjA== Date: Mon, 23 Oct 2023 17:19:12 +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: <816030ec-32a7-4be0-816d-6764fd5dbb8e@proton.me> In-Reply-To: <878r7thh3w.fsf@metaspace.dk> References: <20231019171540.259173-1-benno.lossin@proton.me> <87fs25irel.fsf@metaspace.dk> <878r7thh3w.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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Mon, 23 Oct 2023 10:19:32 -0700 (PDT) On 23.10.23 10:30, Andreas Hindborg (Samsung) wrote: >=20 > Benno Lossin writes: >=20 >> On 20.10.23 11:06, Andreas Hindborg (Samsung) wrote: >>> Benno Lossin writes: >>>> +/// Error message for calling a default function of a [`#[vtable]`](m= acros::vtable) trait. >>>> +pub const VTABLE_DEFAULT_ERROR: &str =3D >>>> + "This function must not be called, see the #[vtable] documentatio= n."; >>>> 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 typica= lly use C >>>> /// `NULL` pointers to represent these functions. >>>> /// >>>> -/// This attribute is intended to close the gap. Traits can be declar= ed and >>>> -/// implemented with the `#[vtable]` attribute, and a `HAS_*` associa= ted 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 tra= it with `#[vtable]`. This >>>> +/// attribute generates a `HAS_*` associated constant bool for each m= ethod 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 implementat= ion. 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 t= o replicate the default >>>> +/// behavior in Rust, the default implementation should be: >>> >>> How about this?: >>> >>> 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 sid= e >>> vtable, and thus the Rust default implementation can not be called. The >>> default implementation should be: >>> >>> 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 t= his >> is that the functions will be called through function pointers installed= in >> C side vtables. When an optional method is not implemented on a `#[vtabl= e]` >> trait, a NULL entry is installed in the vtable. Thus the default >> implementation is never called. Since these traits are not designed to b= e >> used on the Rust side, it should not be possible to call the default >> implementation. >=20 >> It is not possible to replicate the default behavior from C >> in Rust, since that is not maintainable. >=20 > I don't feel that this bit should be included. It's not a matter of > maintainability. Why would we reimplement something that is already > present in a subsystem? The functionality is already present, so we use > it. But we don't use it on the Rust side. You cannot write this: fn foo(t: &T) -> Result { t.seek(0)? } if the `seek` function is optional. --=20 Cheers, Benno