-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
137 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "mindustry_logic_bang_lang" | ||
version = "0.12.4" | ||
version = "0.12.5" | ||
edition = "2021" | ||
|
||
authors = ["A4-Tacks <[email protected]>"] | ||
|
@@ -18,6 +18,7 @@ lalrpop = "0.20.0" | |
tag_code = { path = "./tools/tag_code", version = "*" } | ||
display_source = { path = "./tools/display_source", version = "*" } | ||
var_utils = { path = "./tools/var_utils", version = "*"} | ||
utils = { path = "./tools/utils", version = "*" } | ||
|
||
[dependencies.lalrpop-util] | ||
version = "0.20.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "utils" | ||
version = "0.1.1" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// 一个负责获取结果并自增的结构 | ||
/// # Examples | ||
/// ``` | ||
/// # use utils::counter::Counter; | ||
/// let mut counter: Counter<_> = Counter::new(|n| { | ||
/// let old = *n; | ||
/// *n += 1; | ||
/// format!("__{old}") | ||
/// }); | ||
/// assert_eq!(counter.get(), "__0"); | ||
/// assert_eq!(counter.get(), "__1"); | ||
/// assert_eq!(counter.get(), "__2"); | ||
/// assert_eq!(counter.get(), "__3"); | ||
/// ``` | ||
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)] | ||
pub struct Counter<F, T = usize> | ||
{ | ||
counter: T, | ||
getter: F, | ||
} | ||
|
||
impl<F, R, T> Counter<F, T> | ||
where F: FnMut(&mut T) -> R | ||
{ | ||
pub fn with_counter(f: F, counter: T) -> Self { | ||
Self { | ||
counter, | ||
getter: f | ||
} | ||
} | ||
|
||
pub fn get(&mut self) -> R { | ||
(self.getter)(&mut self.counter) | ||
} | ||
} | ||
|
||
impl<F, R, T> Counter<F, T> | ||
where F: FnMut(&mut T) -> R, | ||
T: Default | ||
{ | ||
pub fn new(f: F) -> Self { | ||
Self::with_counter(f, Default::default()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test() { | ||
let mut counter: Counter<_> = Counter::new(|n| { | ||
let old = *n; | ||
*n += 1; | ||
format!("__{old}") | ||
}); | ||
assert_eq!(counter.get(), "__0"); | ||
assert_eq!(counter.get(), "__1"); | ||
assert_eq!(counter.get(), "__2"); | ||
assert_eq!(counter.get(), "__3"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! 一系列通用工具 | ||
pub mod counter; |