boxpin 0.1.0

A tiny extension trait that exposes Box::pin as a .pinned() suffix.
Documentation
//! `boxpin` adds a `.pinned()` suffix for values that should become
//! [`Pin<Box<T>>`](std::pin::Pin).
//!
//! The method is intentionally tiny and does exactly what it looks like:
//! it forwards to [`Box::pin`].
//!
//! # Examples
//!
//! Pin an async block with suffix syntax:
//!
//! ```
//! use boxpin::BoxPinExt;
//!
//! # futures::executor::block_on(async {
//! let value = async { 41_u8 }.pinned().await;
//! assert_eq!(value, 41);
//! # });
//! ```
//!
//! Pin a regular value:
//!
//! ```
//! use boxpin::BoxPinExt;
//!
//! let pinned = String::from("boxpin").pinned();
//! assert_eq!(&*pinned, "boxpin");
//! ```

use std::pin::Pin;

/// Extension trait that exposes [`Box::pin`] as the `.pinned()` suffix.
pub trait BoxPinExt: Sized {
    /// Wrap `self` in [`Pin<Box<_>>`](Pin) using [`Box::pin`].
    fn pinned(self) -> Pin<Box<Self>>;
}

impl<T> BoxPinExt for T
where
    T: Sized,
{
    fn pinned(self) -> Pin<Box<Self>> {
        Box::pin(self)
    }
}

#[cfg(test)]
mod tests {
    use super::BoxPinExt;
    use futures::executor::block_on;

    #[test]
    fn pins_plain_values() {
        let pinned = String::from("boxpin").pinned();

        assert_eq!(&*pinned, "boxpin");
    }

    #[test]
    fn pins_async_blocks() {
        let future = async { 7_u64 }.pinned();

        assert_eq!(block_on(future), 7);
    }
}