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

ndk: Implement common traits where sensible, and drop some Ord #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MarijnS95
Copy link
Member

@MarijnS95 MarijnS95 commented Aug 10, 2024

Provided traits in the ndk crate are all over the place. Some pointer wrappers and regular enumerations derive (Partial)Ord even though there is no sense in using an ordering for these types. Others don't derive (Partial)Eq and Hash which makes it hard to compare if objects are the same (by-pointer) or to store them inside i.e. HashMap. Deriving these types follows Rust's C-COMMON-TRAITS convention.

Additionally, sort derives by their relation, followed by sorting them alphabetically.

Note that Hash may not be all too useful to store owned types as keys in HashMap, in case you have to temporarily increase the refcount on a pointer (i.e. a NativeWindow inside a callback via NativeWindow::clone_from_ptr()) just to extract the element with that pointer, but that is hopefully something we can revise with #309. Additionally, some ways of solving that issue may involve adding Clone, Copy to some non-refcounted types (assuming they have an external lifetime guarding it).

TODO

  • Enable deriving some traits in ndk-sys to be able to implement them on structures that own ffi structures;
  • Derive comparison traits on objects that keep callbacks for lifetime purposes?
  • Omit derives on Iter wrappers?
  • Have Ord on pointer wrappers after all, to store them in i.e. BTreeMap even though their ordering has no useful definition?

CC @daxpedda for suggestions based on rust-windowing/winit#3796 :)

ndk/src/media/media_codec.rs Show resolved Hide resolved
Provided traits in the `ndk` crate are all over the place.  Some
pointer wrappers and regular enumerations derive `(Partial)Ord` even
though there is no sense in using an ordering for these types.  Others
don't derive `(Partial)Eq` and `Hash` which makes it hard to compare
if objects are the same (by-pointer) or to store them inside i.e.
`HashMap`.  Deriving these types follows Rust's [C-COMMON-TRAITS]
convention.

Additionally, sort `derives` by their relation, followed by sorting
them alphabetically.

[C-COMMON-TRAITS]: https://rust-lang.github.io/api-guidelines/interoperability.html#types-eagerly-implement-common-traits-c-common-traits
Comment on lines +17 to 19
#[derive(Debug, PartialEq, Eq, Hash)]
#[doc(alias = "AAssetManager")]
pub struct AssetManager {
Copy link
Member Author

@MarijnS95 MarijnS95 Sep 3, 2024

Choose a reason for hiding this comment

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

Can be Clone/Copy since this is a cheap reference/handle from Activity (but should carry a lifetime on Activity realistically).

EDIT: Turns out I already started looking into this at https://github.com/rust-mobile/ndk/compare/asset-lifetime

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.

2 participants