zbus/abstractions/
file.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! Runtime-agnostic File I/O abstractions.
//!
//! Proving only specific API that we need internally.

#[cfg(unix)]
use std::fs::Metadata;
use std::{
    io::Result,
    path::Path,
    pin::Pin,
    task::{Context, Poll},
};

use futures_core::Stream;

#[cfg(not(feature = "tokio"))]
#[derive(Debug)]
pub struct FileLines(futures_util::io::Lines<futures_util::io::BufReader<async_fs::File>>);
#[cfg(feature = "tokio")]
#[derive(Debug)]
pub struct FileLines(tokio::io::Lines<tokio::io::BufReader<tokio::fs::File>>);

impl FileLines {
    pub async fn open(path: impl AsRef<Path>) -> Result<Self> {
        #[cfg(not(feature = "tokio"))]
        {
            async_fs::File::open(path)
                .await
                .map(futures_util::io::BufReader::new)
                .map(futures_util::AsyncBufReadExt::lines)
                .map(Self)
        }

        #[cfg(feature = "tokio")]
        {
            tokio::fs::File::open(path)
                .await
                .map(tokio::io::BufReader::new)
                .map(tokio::io::AsyncBufReadExt::lines)
                .map(Self)
        }
    }
}

impl Stream for FileLines {
    type Item = Result<String>;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        #[cfg(not(feature = "tokio"))]
        {
            Stream::poll_next(Pin::new(&mut self.get_mut().0), cx)
        }

        #[cfg(feature = "tokio")]
        {
            let fut = self.get_mut().0.next_line();
            futures_util::pin_mut!(fut);
            std::future::Future::poll(Pin::new(&mut fut), cx).map(Result::transpose)
        }
    }

    #[cfg(not(feature = "tokio"))]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.0.size_hint()
    }
}

// Not unix-specific itself but only used on unix.
#[cfg(unix)]
pub async fn metadata(path: impl AsRef<Path>) -> Result<Metadata> {
    #[cfg(not(feature = "tokio"))]
    {
        async_fs::metadata(path).await
    }

    #[cfg(feature = "tokio")]
    {
        tokio::fs::metadata(path).await
    }
}