-
Notifications
You must be signed in to change notification settings - Fork 471
platform: move get_id_info from platform.rs to public #864
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
base: rust
Are you sure you want to change the base?
Conversation
534cfc4
to
263a1fe
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two other options to consider:
- Create a standalone function that can be used by any adapter.
- Create a bang macro used inside the impl. So for example
impl<T: Driver> Adapter<T> { impl_get_id_info!(); /* ... */ }
.
I think I prefer the first option.
rust/macros/of_id.rs
Outdated
|
||
format!( | ||
" | ||
impl<T: Driver> {name}<T> {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[derive(Foo)]
generally implements a trait called Foo
for the respective type. Not sure if we should introduce a trait GetIdInfo
, or not.
@Richardhongyu thanks for doing this. What I had in mind, however, was similar to what @bjorn3 said: a function that other adapters can call. Is there a reason why you think such an option wouldn't work? |
I tried this before. But the |
Then option 2
Should work. |
Option 2 looks easier to read. I will change to this style. |
263a1fe
to
ce42dbd
Compare
The new commit uses Option 2. |
ce42dbd
to
b52bafa
Compare
@bjorn3 Thanks for reviewing and helping improve the code! |
b52bafa
to
8495bc9
Compare
I pushed a new version to apply the above changes. |
8495bc9
to
59bff0e
Compare
Why can't these be passed in as arguments? |
Here's a working version (just the first few lines are different, the rest is exactly the same): fn get_id_info<T>(
dev: &impl RawDevice,
table: Option<driver::IdTable<'static, of::DeviceId, T>>,
) -> Option<&'static T> {
let table = table?;
// SAFETY: `table` has static lifetime, so it is valid for read. `dev` is guaranteed to be
// valid while it's alive, so is the raw device returned by it.
let id = unsafe { bindings::of_match_device(table.as_ref(), dev.raw_device()) };
if id.is_null() {
return None;
}
// SAFETY: `id` is a pointer within the static table, so it's always valid.
let offset = unsafe { (*id).data };
if offset.is_null() {
return None;
}
// SAFETY: The offset comes from a previous call to `offset_from` in `IdArray::new`, which
// guarantees that the resulting pointer is within the table.
let ptr = unsafe { id.cast::<u8>().offset(offset as _).cast::<Option<T>>() };
// SAFETY: The id table has a static lifetime, so `ptr` is guaranteed to be valid for read.
unsafe { (&*ptr).as_ref() }
}
let info = get_id_info(&dev, T::OF_DEVICE_ID_TABLE); Do you see a reason why this wouldn't work? |
59bff0e
to
cf1679d
Compare
93551a0
to
260a52f
Compare
Signed-off-by: Li Hongyu <[email protected]>
260a52f
to
c13e4d1
Compare
@wedsonaf This is really cool! Thanks for your help! I didn't think of passing |
Signed-off-by: Li Hongyu [email protected]