3 releases
0.1.2 | Sep 28, 2018 |
---|---|
0.1.1 | Sep 14, 2018 |
0.1.0 | Sep 14, 2018 |
#2761 in Rust patterns
19KB
395 lines
pinpoint
This crate provides the IntoPin
trait. IntoPin
is powerfull for creating coerced, pinned references.
Example
#![feature(pin)]
extern crate pinpoint;
use std::pin::Pin;
use pinpoint::IntoPin;
fn example<'a, P>(item: P)
where
P: IntoPin<&'a mut [u8]>
{
let mut pin: Pin<&mut [u8]> = item.into_pin();
pin.reverse();
}
let mut v = vec![1, 2, 3, 4];
example(&mut v);
assert_eq!(v, [4, 3, 2, 1]);
let mut b: Box<[u8]> = Box::new([4, 3, 2, 1]);
example(&mut b);
assert_eq!(*b, [1, 2, 3, 4]);
lib.rs
:
This crate provides the IntoPin
trait.
IntoPin
can be used to wrap any type in a Pin
,
but is powerfull in creating coerced, pinned references.
Examples
#![feature(pin)]
extern crate pinpoint;
use std::pin::Pin;
use pinpoint::IntoPin;
let v = vec![1, 2, 3, 4, 5];
let pinned_slice: Pin<&[u32]> = (&v).into_pin();
An example using generics:
#![feature(pin)]
extern crate pinpoint;
use std::pin::Pin;
use pinpoint::IntoPin;
fn example<'a, P>(item: P)
where
P: IntoPin<&'a mut [u8]>
{
let mut pin = item.into_pin();
pin.reverse();
}
let mut v = vec![1, 2, 3, 4];
example(&mut v);
assert_eq!(v, [4, 3, 2, 1]);
let mut b: Box<[u8]> = Box::new([4, 3, 2, 1]);
example(&mut b);
assert_eq!(*b, [1, 2, 3, 4]);
Features
In order to use the IntoPin
trait, this crate should be used with the feature pinned
of this crate turned on.
In order to create a pinned slice containg Cell types from a Cell containing a slice, use the slice_of_cells
feature of this crate.