Skip to content

Commit 0f9c6ad

Browse files
authored
Merge pull request #277 from Tools-cx-app/offical/opt-code
Using where to improve readability
2 parents e040093 + ef43b09 commit 0f9c6ad

File tree

8 files changed

+58
-46
lines changed

8 files changed

+58
-46
lines changed

src/file_handler.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,11 @@ impl FileHandler {
3838
}
3939
}
4040

41-
pub fn read_to_string(&mut self, path: impl AsRef<Path>) -> Result<String> {
42-
let mut string = String::new();
43-
match self.files.entry(path.as_ref().to_path_buf()) {
44-
Entry::Occupied(mut entry) => {
45-
let mut string = String::new();
46-
entry.get_mut().rewind()?;
47-
entry.get().read_to_string(&mut string)?;
48-
}
49-
Entry::Vacant(entry) => {
50-
let mut file = File::open(path.as_ref())?;
51-
file.read_to_string(&mut string)?;
52-
entry.insert(file);
53-
}
54-
}
55-
56-
Ok(string)
57-
}
58-
59-
pub fn write_with_workround(
60-
&mut self,
61-
path: impl AsRef<Path>,
62-
content: impl AsRef<[u8]>,
63-
) -> Result<()> {
41+
pub fn write_with_workround<P, S>(&mut self, path: P, content: S) -> Result<()>
42+
where
43+
P: AsRef<Path>,
44+
S: AsRef<[u8]>,
45+
{
6446
if let Err(e) = self.write(path.as_ref(), content.as_ref()) {
6547
match e.kind() {
6648
ErrorKind::PermissionDenied => {
@@ -76,7 +58,11 @@ impl FileHandler {
7658
}
7759
}
7860

79-
pub fn write(&mut self, path: impl AsRef<Path>, content: impl AsRef<[u8]>) -> io::Result<()> {
61+
pub fn write<P, S>(&mut self, path: P, content: S) -> io::Result<()>
62+
where
63+
P: AsRef<Path>,
64+
S: AsRef<[u8]>,
65+
{
8066
match self.files.entry(path.as_ref().to_path_buf()) {
8167
Entry::Occupied(mut entry) => {
8268
entry.get_mut().write_all(content.as_ref())?;

src/framework/config/merge.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ struct ConfigData {
3333
}
3434

3535
impl Config {
36-
pub fn merge<S: AsRef<str>>(l: S, s: S) -> Result<String> {
36+
pub fn merge<S>(l: S, s: S) -> Result<String>
37+
where
38+
S: AsRef<str>,
39+
{
3740
let local_conf = l.as_ref();
3841
let std_conf = s.as_ref();
3942

src/framework/config/mod.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use log::{error, info};
2727
use toml::Value;
2828

2929
use crate::framework::{error::Result, node::Mode};
30-
pub use data::{Config as ConfigConfig, ConfigData, MarginFps, ModeConfig, TemperatureThreshold};
30+
pub use data::{ConfigData, MarginFps, ModeConfig, TemperatureThreshold};
3131
use read::wait_and_read;
3232

3333
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -42,7 +42,10 @@ pub struct Config {
4242
}
4343

4444
impl Config {
45-
pub fn new<P: AsRef<Path>>(p: P, sp: P) -> Result<Self> {
45+
pub fn new<P>(p: P, sp: P) -> Result<Self>
46+
where
47+
P: AsRef<Path>,
48+
{
4649
let path = p.as_ref();
4750
let std_path = sp.as_ref();
4851
let toml_raw = fs::read_to_string(path)?;
@@ -68,14 +71,20 @@ impl Config {
6871
Ok(Self { inner })
6972
}
7073

71-
pub fn need_fas<S: AsRef<str>>(&mut self, pkg: S) -> bool {
74+
pub fn need_fas<S>(&mut self, pkg: S) -> bool
75+
where
76+
S: AsRef<str>,
77+
{
7278
let pkg = pkg.as_ref();
7379

7480
self.inner.config().game_list.contains_key(pkg)
7581
|| self.inner.config().scene_game_list.contains(pkg)
7682
}
7783

78-
pub fn target_fps<S: AsRef<str>>(&mut self, pkg: S) -> Option<TargetFps> {
84+
pub fn target_fps<S>(&mut self, pkg: S) -> Option<TargetFps>
85+
where
86+
S: AsRef<str>,
87+
{
7988
let pkg = pkg.as_ref();
8089
let pkg = pkg.split(':').next()?;
8190

@@ -125,9 +134,4 @@ impl Config {
125134
Mode::Fast => &self.inner.config().fast,
126135
}
127136
}
128-
129-
#[must_use]
130-
pub fn config(&mut self) -> ConfigConfig {
131-
self.inner.config().config
132-
}
133137
}

src/framework/extension/api/misc.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ pub fn get_api_version(lua: &Lua) -> u8 {
2525
lua.globals().get("API_VERSION").unwrap_or(0)
2626
}
2727

28-
pub fn do_callback<P: AsRef<Path>, S: AsRef<str>, A: IntoLuaMulti>(
29-
extension: P,
30-
lua: &Lua,
31-
function: S,
32-
args: A,
33-
) {
28+
pub fn do_callback<P, S, A>(extension: P, lua: &Lua, function: S, args: A)
29+
where
30+
P: AsRef<Path>,
31+
S: AsRef<str>,
32+
A: IntoLuaMulti,
33+
{
3434
let function = function.as_ref();
3535
let extension = extension.as_ref();
3636

src/framework/node/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ impl Node {
3434
Ok(result)
3535
}
3636

37-
pub fn create_node<S: AsRef<str>>(&mut self, i: S, d: S) -> Result<()> {
37+
pub fn create_node<S>(&mut self, i: S, d: S) -> Result<()>
38+
where
39+
S: AsRef<str>,
40+
{
3841
let id = i.as_ref();
3942
let default = d.as_ref();
4043

@@ -43,7 +46,10 @@ impl Node {
4346
self.refresh()
4447
}
4548

46-
pub fn remove_node<S: AsRef<str>>(&mut self, i: S) -> Result<()> {
49+
pub fn remove_node<S>(&mut self, i: S) -> Result<()>
50+
where
51+
S: AsRef<str>,
52+
{
4753
let id = i.as_ref();
4854

4955
let path = Path::new(NODE_PATH).join(id);
@@ -52,7 +58,10 @@ impl Node {
5258
self.refresh()
5359
}
5460

55-
pub fn get_node<S: AsRef<str>>(&mut self, id: S) -> Result<String> {
61+
pub fn get_node<S>(&mut self, id: S) -> Result<String>
62+
where
63+
S: AsRef<str>,
64+
{
5665
let id = id.as_ref();
5766

5867
if unlikely(self.timer.elapsed() > REFRESH_TIME) {

src/framework/scheduler/looper/clean.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ use libc::{MS_BIND, MS_REC, mount, umount, umount2};
2828

2929
use crate::framework::error::Result;
3030

31-
fn lock_value<P: AsRef<Path>, S: AsRef<str>>(path: P, value: S) {
31+
fn lock_value<P, S>(path: P, value: S)
32+
where
33+
P: AsRef<Path>,
34+
S: AsRef<str>,
35+
{
3236
let value = value.as_ref();
3337
let path = path.as_ref();
3438

src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ fn main() -> Result<()> {
7878
Ok(())
7979
}
8080

81-
fn run<S: AsRef<str>>(std_path: S) -> Result<()> {
81+
fn run<S>(std_path: S) -> Result<()>
82+
where
83+
S: AsRef<str>,
84+
{
8285
#[cfg(not(debug_assertions))]
8386
let logger_spec = LogSpecification::info();
8487

src/misc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
use std::process::Command;
1919

20-
pub fn setprop<S: AsRef<str>>(k: S, v: S) {
20+
pub fn setprop<S>(k: S, v: S)
21+
where
22+
S: AsRef<str>,
23+
{
2124
let key = k.as_ref();
2225
let value = v.as_ref();
2326
let _ = Command::new("setprop").args([key, value]).spawn();

0 commit comments

Comments
 (0)