Il mar 17 dic 2024, 16:55 Zhao Liu <zhao1....@intel.com> ha scritto:

> On Mon, Dec 09, 2024 at 01:37:17PM +0100, Paolo Bonzini wrote:
> > Date: Mon,  9 Dec 2024 13:37:17 +0100
> > From: Paolo Bonzini <pbonz...@redhat.com>
> > Subject: [PATCH 26/26] rust: callbacks: allow passing optional callbacks
> as
> >  ()
> > X-Mailer: git-send-email 2.47.1
> >
> > In some cases, callbacks are optional.  Using "Some(function)" and "None"
> > does not work well, because when someone writes "None" the compiler does
> > not know what to use for "F" in "Option<F>".
>
> I understand the direct use case is MemoryRegionOps, which has optional
> callbacks. However, I'm not quite sure how exactly it should be applied
> to C bindings and how it will play with Option<callback>.
>

You wouldn't use Option<callback> at all, using () instead of None; the
difference is that () does not have a parameter while None does (and the
compiler cannot infer it). But I am okay with leaving this patch behind
until there's a need.

Paolo

Could u pls provide a simple example?
>
> > Therefore, adopt () to mean a "null" callback.  It is possible to enforce
> > that a callback is valid by adding a "let _: () = F::ASSERT_IS_SOME"
> before
> > the invocation of F::call.
> >
> > Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
> > ---
> >  rust/qemu-api/src/callbacks.rs | 97 ++++++++++++++++++++++++++++++++++
> >  1 file changed, 97 insertions(+)
> >
> > diff --git a/rust/qemu-api/src/callbacks.rs b/rust/qemu-api/src/
> callbacks.rs
> > index 6401d807198..83c681d6478 100644
> > --- a/rust/qemu-api/src/callbacks.rs
> > +++ b/rust/qemu-api/src/callbacks.rs
> > @@ -76,6 +76,31 @@
> >  /// call_it(&move |_| String::from(x), "hello workd");
>
> typo: s/workd/word/ (in previous patch :-))
>
>

Reply via email to