tokio_fs/file/
clone.rs

1use super::File;
2
3use futures::{Future, Poll};
4
5use std::io;
6
7/// Future returned by `File::try_clone`.
8///
9/// Clones a file handle into two file handles.
10///
11/// # Panics
12///
13/// Will panic if polled after returning an item or error.
14#[derive(Debug)]
15pub struct CloneFuture {
16    file: Option<File>,
17}
18
19impl CloneFuture {
20    pub(crate) fn new(file: File) -> Self {
21        Self { file: Some(file) }
22    }
23}
24
25impl Future for CloneFuture {
26    type Item = (File, File);
27    type Error = (File, io::Error);
28
29    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
30        self.file
31            .as_mut()
32            .expect("Cannot poll `CloneFuture` after it resolves")
33            .poll_try_clone()
34            .map(|inner| inner.map(|cloned| (self.file.take().unwrap(), cloned)))
35            .map_err(|err| (self.file.take().unwrap(), err))
36    }
37}