cdns_rs/a_sync/tokio_exc/
async_intrf.rs

1use tokio::{fs::File, io::AsyncReadExt, sync::{Mutex, MutexGuard}};
2
3use crate::{a_sync::{caches::CacheInstance, interface::{AsyncMutex, AsyncMutexGuard, MutexedCaches, UnifiedFs}}, cfg_host_parser::HostConfig, ResolveConfig};
4
5#[derive(Debug)]
6pub struct TokioInterf;
7
8impl MutexedCaches for TokioInterf
9{
10    type MetadataFs = File;
11
12    type ResolveCache = Mutex<CacheInstance<ResolveConfig, Self::MetadataFs>>;
13
14    type HostCahae = Mutex<CacheInstance<HostConfig, Self::MetadataFs>>;
15}
16
17impl UnifiedFs for File
18{
19    type ErrRes = std::io::Error;
20
21    type FileOp = Self;
22
23    async 
24    fn metadata(path: &std::path::Path) -> Result<std::fs::Metadata, Self::ErrRes> 
25    {
26        return tokio::fs::metadata(path).await;
27    }
28
29    async 
30    fn open<P: AsRef<std::path::Path>>(path: P) -> std::io::Result<Self::FileOp> 
31    {
32        return tokio::fs::File::open(path).await;
33    }
34
35    async 
36    fn read_to_string(file: &mut Self::FileOp, buf: &mut String) -> std::io::Result<usize> 
37    {
38        return file.read_to_string(buf).await;
39    }
40}
41
42
43impl<DS: Sized> AsyncMutex<DS> for Mutex<DS>
44{
45    type MutxGuard<'mux> = MutexGuard<'mux, DS> where DS: 'mux;
46
47    fn a_new(v: DS) -> Self 
48    {
49        return Mutex::new(v);
50    }
51    
52    fn a_lock<'mux>(&'mux self) -> impl Future<Output = Self::MutxGuard<'mux>> 
53    {
54        return self.lock();
55    }
56}
57
58impl<'mux, DS: Sized> AsyncMutexGuard<'mux, DS>for MutexGuard<'mux, DS>
59{
60    fn guard(&self) -> &DS
61    {
62        return self;
63    }
64
65    fn guard_mut(&mut self) -> &mut DS
66    {
67        return self;
68    }
69}