Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error[E0308]: mismatched types #1399

Open
arcnmx opened this issue Nov 9, 2022 · 4 comments
Open

error[E0308]: mismatched types #1399

arcnmx opened this issue Nov 9, 2022 · 4 comments

Comments

@arcnmx
Copy link

arcnmx commented Nov 9, 2022

gir generates ptr::null_mut() for &mut type_ instead of ptr::null() for a function that returns a const ptr (const gchar** argument type):

error[E0308]: mismatched types
src/auto/metadata.rs:71:123

impl<O: IsA<Metadata>> MetadataExt for O {
    fn find(&self, subject: u32, key: &str) -> (glib::GString, glib::GString) {
        unsafe {
            let mut type_ = ptr::null_mut();
            let ret = from_glib_none(ffi::wp_metadata_find(self.as_ref().to_glib_none().0, subject, key.to_glib_none().0, &mut type_));
            //                       --------------------- arguments to this function are incorrect                       ^^^^^^^^^^ types differ in muability
            (ret, from_glib_full(type_))
        }
    }
}

gir source

      <method name="find" c:identifier="wp_metadata_find">
        <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1191">Finds the metadata value given its @subject and @key.</doc>
        <source-position filename="../lib/wp/metadata.h" line="50"/>
        <return-value transfer-ownership="none">
          <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1203">the metadata string value, or NULL if not found.</doc>
          <type name="utf8" c:type="const gchar*"/>
        </return-value>
        <parameters>
          <instance-parameter name="self" transfer-ownership="none">
            <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1193">a metadata object</doc>
            <type name="Metadata" c:type="WpMetadata*"/>
          </instance-parameter>
          <parameter name="subject" transfer-ownership="none">
            <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1194">the metadata subject id</doc>
            <type name="guint32" c:type="guint32"/>
          </parameter>
          <parameter name="key" transfer-ownership="none">
            <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1195">the metadata key name</doc>
            <type name="utf8" c:type="const gchar*"/>
          </parameter>
          <parameter name="type" direction="out" caller-allocates="0" transfer-ownership="full" optional="1" allow-none="1">
            <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1196">the metadata type name</doc>
            <type name="utf8" c:type="const gchar**"/>
          </parameter>
        </parameters>
      </method>
@bilelmoussaoui
Copy link
Member

How would you write into a ptr::null()?

@arcnmx
Copy link
Author

arcnmx commented Dec 10, 2022

well it's a (const gchar*)* argument, so unless I'm mistaken, just swapping null_mut out for null in the example above should be correct?

so it becomes:

    let mut type_ = ptr::null();
    ffi::stuff(&mut type_); // this coerces to `&mut type_ as *mut (*const gchar)`

EDIT: sort of like what @sdroege said below, except it's just a simple output parameter returning a single string, there's no Vec involved afaict

EDIT2: relevant documentation for this specific function

@sdroege
Copy link
Member

sdroege commented Dec 10, 2022

I think in Rust terms what this function returns is a *mut *const c_char or Vec<&'static glib::GStr> or so. The strings themselves are const but the returned array of const strings is not?

So this is more a transfer container than transfer full situation. And also needs an array zero-terminated=1 annotation in C


EDIT: This actually returns a single static string. So this is all correct and should've been a ptr::null() here. However you probably want to implement this one manually for now to avoid the useless allocation. This can just return a &'static glib::GStr.

@sdroege
Copy link
Member

sdroege commented Dec 10, 2022

          <doc xml:space="preserve" filename="../docs/wp-gtkdoc.h" line="1203">the metadata string value, or NULL if not found.</doc>

This part, the return value, is missing a nullable annotation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants