linux-stable/rust/kernel
Obei Sideg 5bc818419a rust: types: add try_from_foreign() method
Currently `ForeignOwnable::from_foreign()` only works for non-null
pointers for the existing `impl`s (e.g. `Box`, `Arc`). In turn, this
means callers may write code like:

```rust
// `p` is a pointer that may be null.
if p.is_null() {
    None
} else {
    unsafe { Some(Self::from_foreign(ptr)) }
}
```

Add a `try_from_foreign()` method to the trait with a default
implementation that returns `None` if `ptr` is null, otherwise
`Some(from_foreign(ptr))`, so that it can be used by callers instead.

Link: https://github.com/Rust-for-Linux/linux/issues/1057
Signed-off-by: Obei Sideg <linux@obei.io>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Trevor Gross <tmgross@umich.edu>
Link: https://lore.kernel.org/r/0100018d53f737f8-80c1fe97-0019-40d7-ab69-b1b192785cd7-000000@email.amazonses.com
[ Fixed intra-doc links, improved `SAFETY` comment and reworded commit. ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2024-02-25 19:17:31 +01:00
..
init rust: init: update expanded macro explanation 2023-08-21 14:31:49 +02:00
net rust: net::phy add module_phy_driver macro 2023-12-15 09:35:50 +00:00
sync rust: kernel: stop using ptr_metadata feature 2024-02-18 21:22:27 +01:00
allocator.rs rust: kernel: fix multiple typos in documentation 2024-02-18 21:22:27 +01:00
build_assert.rs rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
error.rs rust: error: improve unsafe code in example 2024-02-18 21:22:27 +01:00
init.rs rust: kernel: add reexports for macros 2024-02-18 21:22:27 +01:00
ioctl.rs rust: kernel: mark code fragments in docs with backticks 2024-02-18 21:22:27 +01:00
kunit.rs rust: support srctree-relative links 2023-12-21 20:54:17 +01:00
lib.rs rust: kernel: stop using ptr_metadata feature 2024-02-18 21:22:27 +01:00
net.rs rust: core abstractions for network PHY drivers 2023-12-15 09:35:50 +00:00
prelude.rs rust: add derive macro for Zeroable 2023-08-21 14:31:48 +02:00
print.rs rust: support srctree-relative links 2023-12-21 20:54:17 +01:00
static_assert.rs rust: static_assert: add static_assert! macro 2022-12-04 01:59:16 +01:00
std_vendor.rs rust: upgrade to Rust 1.68.2 2023-05-31 17:35:03 +02:00
str.rs rust: str: move SAFETY comment in front of unsafe block 2024-02-18 21:22:27 +01:00
sync.rs rust: kernel: add reexports for macros 2024-02-18 21:22:27 +01:00
task.rs rust: kernel: mark code fragments in docs with backticks 2024-02-18 21:22:27 +01:00
time.rs rust: time: add msecs to jiffies conversion 2024-01-28 19:50:48 +01:00
types.rs rust: types: add try_from_foreign() method 2024-02-25 19:17:31 +01:00
workqueue.rs rust: kernel: add reexports for macros 2024-02-18 21:22:27 +01:00