From 1dd39a03f7bc8a64988c8cc575483b02699c7c3e Mon Sep 17 00:00:00 2001 From: codeironman <102459931+codeironman@users.noreply.github.com> Date: Fri, 2 Feb 2024 17:48:09 +0800 Subject: [PATCH] feat: fix use code to adapt to latest Rust version (#325) * Fix #6 * fix #6 * Fix : #6 * Fix : #6 * fix #6 * Update README.md --------- Co-authored-by: caofengyi Co-authored-by: hsluoyz --- src/adapter/file_adapter.rs | 36 ++++++++++++++++++------------------ src/config.rs | 36 +++++++++++++++++++++--------------- src/model/default_model.rs | 6 +++--- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/src/adapter/file_adapter.rs b/src/adapter/file_adapter.rs index e4c65418..ba9042a7 100644 --- a/src/adapter/file_adapter.rs +++ b/src/adapter/file_adapter.rs @@ -8,27 +8,27 @@ use crate::{ #[cfg(feature = "runtime-async-std")] use async_std::{ - fs::File, + fs::File as file, io::prelude::*, - io::{BufReader, Error as IoError, ErrorKind}, - path::Path, + io::{ + BufReader as ioBufReader, Error as ioError, ErrorKind as ioErrorKind, + }, + path::Path as ioPath, prelude::*, }; #[cfg(feature = "runtime-tokio")] use std::{ - io::{Error as IoError, ErrorKind}, - path::Path, + io::{Error as ioError, ErrorKind as ioErrorKind}, + path::Path as ioPath, }; #[cfg(feature = "runtime-tokio")] use tokio::{ - fs::File, - io::{AsyncBufReadExt, AsyncWriteExt, BufReader}, + fs::File as file, + io::{AsyncBufReadExt, AsyncWriteExt, BufReader as ioBufReader}, }; use async_trait::async_trait; - -use std::convert::AsRef; use std::fmt::Write; pub struct FileAdapter

{ @@ -42,7 +42,7 @@ type LoadFilteredPolicyFileHandler<'a> = impl

FileAdapter

where - P: AsRef + Send + Sync, + P: AsRef + Send + Sync, { pub fn new(p: P) -> FileAdapter

{ FileAdapter { @@ -63,8 +63,8 @@ where m: &mut dyn Model, handler: LoadPolicyFileHandler, ) -> Result<()> { - let f = File::open(&self.file_path).await?; - let mut lines = BufReader::new(f).lines(); + let f = file::open(&self.file_path).await?; + let mut lines = ioBufReader::new(f).lines(); #[cfg(feature = "runtime-async-std")] while let Some(line) = lines.next().await { handler(line?, m) @@ -84,8 +84,8 @@ where filter: Filter<'a>, handler: LoadFilteredPolicyFileHandler<'a>, ) -> Result { - let f = File::open(&self.file_path).await?; - let mut lines = BufReader::new(f).lines(); + let f = file::open(&self.file_path).await?; + let mut lines = ioBufReader::new(f).lines(); let mut is_filtered = false; #[cfg(feature = "runtime-async-std")] @@ -106,7 +106,7 @@ where } async fn save_policy_file(&self, text: String) -> Result<()> { - let mut file = File::create(&self.file_path).await?; + let mut file = file::create(&self.file_path).await?; file.write_all(text.as_bytes()).await?; Ok(()) } @@ -115,7 +115,7 @@ where #[async_trait] impl

Adapter for FileAdapter

where - P: AsRef + Send + Sync, + P: AsRef + Send + Sync, { async fn load_policy(&mut self, m: &mut dyn Model) -> Result<()> { self.is_filtered = false; @@ -137,8 +137,8 @@ where async fn save_policy(&mut self, m: &mut dyn Model) -> Result<()> { if self.file_path.as_ref().as_os_str().is_empty() { - return Err(IoError::new( - ErrorKind::Other, + return Err(ioError::new( + ioErrorKind::Other, "save policy failed, file path is empty", ) .into()); diff --git a/src/config.rs b/src/config.rs index a8a3d211..4f8f330e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,21 +3,25 @@ use crate::Result; #[cfg(feature = "runtime-async-std")] use async_std::{ io::prelude::*, - io::{BufReader, Cursor, Error as IoError, ErrorKind}, + io::{ + BufReader as ioBufReader, Cursor as ioCursor, Error as ioError, + ErrorKind as ioErrorKind, + }, }; #[cfg(all(feature = "runtime-async-std", not(target_arch = "wasm32")))] -use async_std::{fs::File, path::Path}; +use async_std::{fs::File as file, path::Path as ioPath}; #[cfg(feature = "runtime-tokio")] -use std::{io::Cursor, path::Path}; +use std::{io::Cursor as ioCursor, path::Path as ioPath}; #[cfg(feature = "runtime-tokio")] use tokio::io::{ - AsyncBufReadExt, AsyncReadExt, BufReader, Error as IoError, ErrorKind, + AsyncBufReadExt, AsyncReadExt, BufReader as ioBufReader, Error as ioError, + ErrorKind as ioErrorKind, }; #[cfg(all(feature = "runtime-tokio", not(target_arch = "wasm32")))] -use tokio::fs::File; +use tokio::fs::File as file; use std::collections::HashMap; @@ -32,7 +36,7 @@ pub(crate) struct Config { impl Config { #[cfg(not(target_arch = "wasm32"))] - pub(crate) async fn from_file>(p: P) -> Result { + pub(crate) async fn from_file>(p: P) -> Result { let mut c = Config { data: HashMap::new(), }; @@ -46,25 +50,27 @@ impl Config { data: HashMap::new(), }; - c.parse_buffer(&mut BufReader::new(Cursor::new(s.as_ref().as_bytes()))) - .await?; + c.parse_buffer(&mut ioBufReader::new(ioCursor::new( + s.as_ref().as_bytes(), + ))) + .await?; Ok(c) } #[cfg(not(target_arch = "wasm32"))] - async fn parse>(&mut self, p: P) -> Result<()> { - let mut f = File::open(p).await?; + async fn parse>(&mut self, p: P) -> Result<()> { + let mut f = file::open(p).await?; let mut c = Vec::new(); f.read_to_end(&mut c).await?; - let mut reader: BufReader> = - BufReader::new(Cursor::new(&c)); + let mut reader: ioBufReader> = + ioBufReader::new(ioCursor::new(&c)); self.parse_buffer(&mut reader).await } async fn parse_buffer( &mut self, - reader: &mut BufReader>, + reader: &mut ioBufReader>, ) -> Result<()> { let mut section = String::new(); @@ -122,8 +128,8 @@ impl Config { .collect(); if option_val.len() != 2 { - return Err(IoError::new( - ErrorKind::Other, + return Err(ioError::new( + ioErrorKind::Other, format!("parse content error, line={}", line), ) .into()); diff --git a/src/model/default_model.rs b/src/model/default_model.rs index 36cdc4c6..c2fe867b 100644 --- a/src/model/default_model.rs +++ b/src/model/default_model.rs @@ -14,10 +14,10 @@ use parking_lot::RwLock; use ritelinked::{LinkedHashMap, LinkedHashSet}; #[cfg(all(feature = "runtime-async-std", not(target_arch = "wasm32")))] -use async_std::path::Path; +use async_std::path::Path as ioPath; #[cfg(feature = "runtime-tokio")] -use std::path::Path; +use std::path::Path as ioPath; use std::{collections::HashMap, sync::Arc}; @@ -28,7 +28,7 @@ pub struct DefaultModel { impl DefaultModel { #[cfg(not(target_arch = "wasm32"))] - pub async fn from_file>(p: P) -> Result { + pub async fn from_file>(p: P) -> Result { let cfg = Config::from_file(p).await?; let mut model = DefaultModel::default();