From: "Andreas Hindborg (Samsung)" <nmi@metaspace.dk>
To: Benno Lossin <benno.lossin@proton.me>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
"Wedson Almeida Filho" <wedsonaf@gmail.com>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rust: macros: improve `#[vtable]` documentation
Date: Mon, 23 Oct 2023 10:30:06 +0200 [thread overview]
Message-ID: <878r7thh3w.fsf@metaspace.dk> (raw)
In-Reply-To: <ba252f66-b204-43c1-9705-8ccd0cb12492@proton.me>
Benno Lossin <benno.lossin@proton.me> writes:
> On 20.10.23 11:06, Andreas Hindborg (Samsung) wrote:
>> Benno Lossin <benno.lossin@proton.me> writes:
>>> +/// 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..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_*` 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.
>>> +///
>>> +/// For a function to be optional, it must have a default implementation. 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 to 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 side
>> vtable, and thus the Rust default implementation can not be called. The
>> default implementation should be:
>>
>> Not sure if it is more clear 🤷
>
> 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.
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.
> The default implementaiton should
> therefore call `kernel::build_error`, thus preventing calls to this
> function at compile time:
Otherwise I think it is good 👍
BR Andreas
next prev parent reply other threads:[~2023-10-23 8:34 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-10-19 17:15 [PATCH v2] rust: macros: improve `#[vtable]` documentation Benno Lossin
2023-10-19 18:49 ` Ariel Miculas (amiculas)
2023-10-20 9:06 ` Andreas Hindborg (Samsung)
2023-10-21 13:16 ` Benno Lossin
2023-10-23 8:30 ` Andreas Hindborg (Samsung) [this message]
2023-10-23 17:19 ` Benno Lossin
2023-10-21 12:45 ` Alice Ryhl
2023-10-23 7:01 ` Benno Lossin
2023-10-25 13:29 ` Alice Ryhl
2023-10-24 11:24 ` Gary Guo
2023-10-24 14:43 ` Benno Lossin
2023-10-25 19:14 ` Gary Guo
2023-10-25 21:34 ` Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=878r7thh3w.fsf@metaspace.dk \
--to=nmi@metaspace.dk \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=wedsonaf@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox