Skip to content

Add gio::Vfs subclass #1726

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

fbrouille
Copy link
Contributor

Allow custom implementation of gio::Vfs

@fbrouille fbrouille force-pushed the gio_vfs_subclass branch 3 times, most recently from 2484ba8 to 8a65537 Compare May 23, 2025 20:02
@sdroege
Copy link
Member

sdroege commented May 25, 2025

Looks good to me except for the two open discussions about the string array.

@fbrouille fbrouille marked this pull request as draft May 28, 2025 18:45
@fbrouille fbrouille marked this pull request as ready for review May 29, 2025 15:24
/// It can be constructed safely from a `&StrV` and unsafely from a pointer to a C array.
/// This type is very similar to `[GStringPtr]`, but with one added constraint: the underlying C array must be `NULL`-terminated.
#[repr(transparent)]
pub struct CStrV {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name seems inconsistent with everything else. This is basically the String/PathBuf (StrV) vs. str/Path (CStrV) duality. Maybe this one should've actually been StrV and the other one StringV. Using Buf here seems wrong.

In other places (e.g. in gstreamer) there's a pattern of Foo and FooRef, so maybe we can just call this one StrVRef? @bilelmoussaoui what do you think?

Also GStringPtr is an ugly name, should probably reconsider that before the next release. Its difference with GStr is that it doesn't store the length and is equivalent with the raw C pointer. GStrLite or so... doesn't have to be solved as part of this PR but in case someone has ideas while we talk about naming anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CStrV because of std::ffi::CStr. However I agree that name should be consistent with others gtk-rs types. I think also that CStrV should be StrV and StrV should be StringV or if you don't want breaking changes, maybe renameCStrV to GStrV and rename StrV to GStringV and create a type alias (deprecated) StrV = GStringV.

Anyway let me know for CStrV new name.

self.parent_get_file_for_uri(uri)
}

unsafe fn get_supported_uri_schemes(&self) -> &'static CStrV {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsafe fn get_supported_uri_schemes(&self) -> &'static CStrV {
fn get_supported_uri_schemes(&self) -> &'static CStrV {

no need to for this to be unsafe anymore (but also if it was necessary then the trait impl would have to be unsafe: unsafe trait is unsafe to implement, unsafe trait function is unsafe to call)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines 225 to 227
let _ = File::for_path("path");
});
glib::gobject_ffi::g_type_from_name("GLocalVfs".to_glib_none().0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that it really matters because this is a test, but why not call g_type_from_name() also inside the Once (and make it a LazyLock)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it does not work. Calling File::for_pathis the only way to initialize GLocalVfs.

Whatever, I have removed binding to existing (private) gio types and refactor the tests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant doing both the File::for_path() and g_type_from_name() inside the LazyLock (or OnceCell)

Comment on lines 274 to 277
static SUPPORTED_URI_SCHEMES: std::sync::OnceLock<glib::StrV> =
std::sync::OnceLock::new();
SUPPORTED_URI_SCHEMES
.get_or_init(|| glib::StrV::from(["myvfs"]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not LazyLock?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be consistent with initialization way of other static variables in the gtk-rs. LazyLock is not (yet) used in the project.

Whatever, I changed it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also seems fine to me. I think I should go over the other code and update it one of these days :) Thanks for the reminder!

@@ -123,6 +123,20 @@ impl std::ops::Deref for StrV {
}
}

impl AsRef<CStrV> for StrV {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably also Deref into &CStrV (which in turn Deref into &[GStringPtr]).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

inner: [GStringPtr],
}

impl CStrV {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably get all the same trait impls that StrV has (PartialEq, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to implement the same traits except Clone and Drop because CStrV does not own the null-terminated array.

This type represents a borrowed reference to a `NULL`-terminated array
of `NULL`-terminated UTF-8 strings.

Signed-off-by: fbrouille <[email protected]>
Signed-off-by: fbrouille <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants