<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux-stable.git/rust/macros/vtable.rs, branch v6.2.2</title>
<subtitle>Linux kernel stable tree</subtitle>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/'/>
<entry>
<title>rust: macros: add `#[vtable]` proc macro</title>
<updated>2022-12-04T00:59:15+00:00</updated>
<author>
<name>Gary Guo</name>
<email>gary@garyguo.net</email>
</author>
<published>2022-11-10T16:41:18+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=b44becc5ee808e02bbda0f90ee0584f206693a33'/>
<id>b44becc5ee808e02bbda0f90ee0584f206693a33</id>
<content type='text'>
This procedural macro attribute provides a simple way to declare
a trait with a set of operations that later users can partially
implement, providing compile-time `HAS_*` boolean associated
constants that indicate whether a particular operation was overridden.

This is useful as the Rust counterpart to structs like
`file_operations` where some pointers may be `NULL`, indicating
an operation is not provided.

For instance:

    #[vtable]
    trait Operations {
        fn read(...) -&gt; Result&lt;usize&gt; {
            Err(EINVAL)
        }

        fn write(...) -&gt; Result&lt;usize&gt; {
            Err(EINVAL)
        }
    }

    #[vtable]
    impl Operations for S {
        fn read(...) -&gt; Result&lt;usize&gt; {
            ...
        }
    }

    assert_eq!(&lt;S as Operations&gt;::HAS_READ, true);
    assert_eq!(&lt;S as Operations&gt;::HAS_WRITE, false);

Signed-off-by: Gary Guo &lt;gary@garyguo.net&gt;
Reviewed-by: Sergio González Collado &lt;sergio.collado@gmail.com&gt;
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda &lt;ojeda@kernel.org&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This procedural macro attribute provides a simple way to declare
a trait with a set of operations that later users can partially
implement, providing compile-time `HAS_*` boolean associated
constants that indicate whether a particular operation was overridden.

This is useful as the Rust counterpart to structs like
`file_operations` where some pointers may be `NULL`, indicating
an operation is not provided.

For instance:

    #[vtable]
    trait Operations {
        fn read(...) -&gt; Result&lt;usize&gt; {
            Err(EINVAL)
        }

        fn write(...) -&gt; Result&lt;usize&gt; {
            Err(EINVAL)
        }
    }

    #[vtable]
    impl Operations for S {
        fn read(...) -&gt; Result&lt;usize&gt; {
            ...
        }
    }

    assert_eq!(&lt;S as Operations&gt;::HAS_READ, true);
    assert_eq!(&lt;S as Operations&gt;::HAS_WRITE, false);

Signed-off-by: Gary Guo &lt;gary@garyguo.net&gt;
Reviewed-by: Sergio González Collado &lt;sergio.collado@gmail.com&gt;
[Reworded, adapted for upstream and applied latest changes]
Signed-off-by: Miguel Ojeda &lt;ojeda@kernel.org&gt;
</pre>
</div>
</content>
</entry>
</feed>
