refactor(lib): Switch from pin-project to pin-project-lite (#2566)

Note the practical affects of this change:

- Dependency count with --features full dropped from 65 to 55.
- Time to compile after a clean dropped from 48s to 35s (on a pretty underpowered VM).

Closes #2388
This commit is contained in:
Jonas Platte
2021-06-04 23:57:27 +02:00
committed by GitHub
parent 0d82405a7b
commit 6a6a24030e
15 changed files with 423 additions and 288 deletions

View File

@@ -1,6 +1,6 @@
use std::mem;
use pin_project::pin_project;
use pin_project_lite::pin_project;
use tokio::sync::watch;
use super::{task, Future, Pin, Poll};
@@ -21,14 +21,15 @@ pub(crate) struct Watch {
rx: watch::Receiver<()>,
}
#[allow(missing_debug_implementations)]
#[pin_project]
pub struct Watching<F, FN> {
#[pin]
future: F,
state: State<FN>,
watch: Pin<Box<dyn Future<Output = ()> + Send + Sync>>,
_rx: watch::Receiver<()>,
pin_project! {
#[allow(missing_debug_implementations)]
pub struct Watching<F, FN> {
#[pin]
future: F,
state: State<FN>,
watch: Pin<Box<dyn Future<Output = ()> + Send + Sync>>,
_rx: watch::Receiver<()>,
}
}
enum State<F> {

View File

@@ -1,4 +1,4 @@
use pin_project::pin_project;
use pin_project_lite::pin_project;
use super::{task, Future, Pin, Poll};
@@ -12,23 +12,27 @@ where
R: Future + Unpin,
{
Lazy {
inner: Inner::Init(func),
inner: Inner::Init { func },
}
}
// FIXME: allow() required due to `impl Trait` leaking types to this lint
#[allow(missing_debug_implementations)]
#[pin_project]
pub(crate) struct Lazy<F, R> {
#[pin]
inner: Inner<F, R>,
pin_project! {
#[allow(missing_debug_implementations)]
pub(crate) struct Lazy<F, R> {
#[pin]
inner: Inner<F, R>,
}
}
#[pin_project(project = InnerProj, project_replace = InnerProjReplace)]
enum Inner<F, R> {
Init(F),
Fut(#[pin] R),
Empty,
pin_project! {
#[project = InnerProj]
#[project_replace = InnerProjReplace]
enum Inner<F, R> {
Init { func: F },
Fut { #[pin] fut: R },
Empty,
}
}
impl<F, R> Started for Lazy<F, R>
@@ -38,8 +42,8 @@ where
{
fn started(&self) -> bool {
match self.inner {
Inner::Init(_) => false,
Inner::Fut(_) | Inner::Empty => true,
Inner::Init { .. } => false,
Inner::Fut { .. } | Inner::Empty => true,
}
}
}
@@ -54,15 +58,15 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
let mut this = self.project();
if let InnerProj::Fut(f) = this.inner.as_mut().project() {
return f.poll(cx);
if let InnerProj::Fut { fut } = this.inner.as_mut().project() {
return fut.poll(cx);
}
match this.inner.as_mut().project_replace(Inner::Empty) {
InnerProjReplace::Init(func) => {
this.inner.set(Inner::Fut(func()));
if let InnerProj::Fut(f) = this.inner.project() {
return f.poll(cx);
InnerProjReplace::Init { func } => {
this.inner.set(Inner::Fut { fut: func() });
if let InnerProj::Fut { fut } = this.inner.project() {
return fut.poll(cx);
}
unreachable!()
}