forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request torvalds#292 from TheSven73/rust-for-linux-pin-wra…
…pper Improve safety with fallible version of `pointer::pin()`
- Loading branch information
Showing
4 changed files
with
37 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
//! Traits useful to drivers, and their implementations for common types. | ||
use core::{ops::Deref, pin::Pin}; | ||
|
||
use alloc::{alloc::AllocError, sync::Arc}; | ||
|
||
/// Trait which provides a fallible version of `pin()` for pointer types. | ||
/// | ||
/// Common pointer types which implement a `pin()` method include [`Box`], [`Arc`] and [`Rc`]. | ||
pub trait TryPin<P: Deref> { | ||
/// Constructs a new `Pin<pointer<T>>`. If `T` does not implement [`Unpin`], then data | ||
/// will be pinned in memory and unable to be moved. An error will be returned | ||
/// if allocation fails. | ||
fn try_pin(data: P::Target) -> core::result::Result<Pin<P>, AllocError>; | ||
} | ||
|
||
impl<T> TryPin<Arc<T>> for Arc<T> { | ||
fn try_pin(data: T) -> core::result::Result<Pin<Arc<T>>, AllocError> { | ||
// SAFETY: the data `T` is exposed only through a `Pin<Arc<T>>`, which | ||
// does not allow data to move out of the `Arc`. Therefore it can | ||
// never be moved. | ||
Ok(unsafe { Pin::new_unchecked(Arc::try_new(data)?) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters