Thread (25 messages) 25 messages, 7 authors, 2023-12-08

Re: [PATCH net-next v9 1/4] rust: core abstractions for network PHY drivers

From: FUJITA Tomonori <fujita.tomonori@gmail.com>
Date: 2023-12-05 03:23:21
Also in: rust-for-linux

On Mon, 4 Dec 2023 18:00:15 -0800
Boqun Feng [off-list ref] wrote:
On Tue, Dec 05, 2023 at 10:14:17AM +0900, FUJITA Tomonori wrote:
[...]
quoted
+    /// Gets the current link state.
+    ///
+    /// It returns true if the link is up.
+    pub fn is_link_up(&self) -> bool {
+        const LINK_IS_UP: u64 = 1;
+        // TODO: the code to access to the bit field will be replaced with automatically
+        // generated code by bindgen when it becomes possible.
+        // SAFETY: The struct invariant ensures that we may access
+        // this field without additional synchronization.
+        let bit_field = unsafe { &(*self.0.get())._bitfield_1 };
+        bit_field.get(14, 1) == LINK_IS_UP
I made a mistake here [1], this should be:

    let bit_field = unsafe { &*(core::ptr::addr_of!((*self.0.get())._bitfield_1)) };
    bit_field.get(14, 1) == LINK_IS_UP

without `core::ptr::add_of!`, `*(self.0.get())` would still create a
temporary `&` to the underlying object I believe. `addr_of!` is the way
to avoid create the temporary reference. Same for the other functions.
If so, how about functions to access to non bit field like phy_id()?

pub fn phy_id(&self) -> u32 {
    let phydev = self.0.get();
    unsafe { (*phydev).phy_id }
}
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help