preset: drain_filter -> extract_if

This commit is contained in:
chyyran 2023-07-20 00:38:13 -04:00 committed by Ronny Chan
parent ab8072c4f7
commit 3735659604
5 changed files with 494 additions and 432 deletions

905
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -14,6 +14,7 @@ members = [
"librashader-capi", "librashader-capi",
"librashader-build-script" "librashader-build-script"
] ]
resolver = "2"
[workspace.metadata.release] [workspace.metadata.release]

View file

@ -7,7 +7,7 @@
//! as input to create a filter chain. //! as input to create a filter chain.
//! //!
//! Re-exported as [`librashader::presets`](https://docs.rs/librashader/latest/librashader/presets/index.html). //! Re-exported as [`librashader::presets`](https://docs.rs/librashader/latest/librashader/presets/index.html).
#![feature(drain_filter)] #![feature(extract_if)]
mod error; mod error;
mod parse; mod parse;

View file

@ -4,7 +4,7 @@ use crate::{ParameterConfig, Scale2D, Scaling, ShaderPassConfig, ShaderPreset, T
pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset { pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
let textures: Vec<TextureConfig> = values let textures: Vec<TextureConfig> = values
.drain_filter(|f| matches!(*f, Value::Texture { .. })) .extract_if(|f| matches!(*f, Value::Texture { .. }))
.map(|value| { .map(|value| {
if let Value::Texture { if let Value::Texture {
name, name,
@ -27,7 +27,7 @@ pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
}) })
.collect(); .collect();
let parameters: Vec<ParameterConfig> = values let parameters: Vec<ParameterConfig> = values
.drain_filter(|f| matches!(*f, Value::Parameter { .. })) .extract_if(|f| matches!(*f, Value::Parameter { .. }))
.map(|value| { .map(|value| {
if let Value::Parameter(name, value) = value { if let Value::Parameter(name, value) = value {
ParameterConfig { name, value } ParameterConfig { name, value }
@ -64,7 +64,7 @@ pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
|v| matches!(*v, Value::Shader(shader_index, _) if shader_index == shader), |v| matches!(*v, Value::Shader(shader_index, _) if shader_index == shader),
) { ) {
let shader_values: Vec<Value> = values let shader_values: Vec<Value> = values
.drain_filter(|v| v.shader_index() == Some(shader)) .extract_if(|v| v.shader_index() == Some(shader))
.collect(); .collect();
let scale_type = shader_values.iter().find_map(|f| match f { let scale_type = shader_values.iter().find_map(|f| match f {
Value::ScaleType(_, value) => Some(*value), Value::ScaleType(_, value) => Some(*value),

View file

@ -187,7 +187,7 @@ fn load_child_reference_strings(
let mut new_tokens = do_lex(&reference_contents)?; let mut new_tokens = do_lex(&reference_contents)?;
let new_references: Vec<PathBuf> = new_tokens let new_references: Vec<PathBuf> = new_tokens
.drain_filter(|token| *token.key.fragment() == "#reference") .extract_if(|token| *token.key.fragment() == "#reference")
.map(|value| PathBuf::from(*value.value.fragment())) .map(|value| PathBuf::from(*value.value.fragment()))
.collect(); .collect();
@ -232,7 +232,7 @@ pub fn parse_values(
} }
let references: Vec<PathBuf> = tokens let references: Vec<PathBuf> = tokens
.drain_filter(|token| *token.key.fragment() == "#reference") .extract_if(|token| *token.key.fragment() == "#reference")
.map(|value| PathBuf::from(*value.value.fragment())) .map(|value| PathBuf::from(*value.value.fragment()))
.collect(); .collect();
@ -254,7 +254,7 @@ pub fn parse_values(
// collect all possible parameter names. // collect all possible parameter names.
let mut parameter_names: Vec<&str> = Vec::new(); let mut parameter_names: Vec<&str> = Vec::new();
for (_, tokens) in all_tokens.iter_mut() { for (_, tokens) in all_tokens.iter_mut() {
for token in tokens.drain_filter(|token| *token.key.fragment() == "parameters") { for token in tokens.extract_if(|token| *token.key.fragment() == "parameters") {
let parameter_name_string: &str = token.value.fragment(); let parameter_name_string: &str = token.value.fragment();
for parameter_name in parameter_name_string.split(';') { for parameter_name in parameter_name_string.split(';') {
parameter_names.push(parameter_name); parameter_names.push(parameter_name);
@ -265,7 +265,7 @@ pub fn parse_values(
// collect all possible texture names. // collect all possible texture names.
let mut texture_names: Vec<&str> = Vec::new(); let mut texture_names: Vec<&str> = Vec::new();
for (_, tokens) in all_tokens.iter_mut() { for (_, tokens) in all_tokens.iter_mut() {
for token in tokens.drain_filter(|token| *token.key.fragment() == "textures") { for token in tokens.extract_if(|token| *token.key.fragment() == "textures") {
let texture_name_string: &str = token.value.fragment(); let texture_name_string: &str = token.value.fragment();
for texture_name in texture_name_string.split(';') { for texture_name in texture_name_string.split(';') {
texture_names.push(texture_name); texture_names.push(texture_name);
@ -276,7 +276,7 @@ pub fn parse_values(
let mut values = Vec::new(); let mut values = Vec::new();
// resolve shader paths. // resolve shader paths.
for (path, tokens) in all_tokens.iter_mut() { for (path, tokens) in all_tokens.iter_mut() {
for token in tokens.drain_filter(|token| parse_indexed_key("shader", token.key).is_ok()) { for token in tokens.extract_if(|token| parse_indexed_key("shader", token.key).is_ok()) {
let (_, index) = parse_indexed_key("shader", token.key).map_err(|e| match e { let (_, index) = parse_indexed_key("shader", token.key).map_err(|e| match e {
nom::Err::Error(e) | nom::Err::Failure(e) => { nom::Err::Error(e) | nom::Err::Failure(e) => {
let input: Span = e.input; let input: Span = e.input;
@ -307,7 +307,7 @@ pub fn parse_values(
// resolve texture paths // resolve texture paths
let mut textures = Vec::new(); let mut textures = Vec::new();
for (path, tokens) in all_tokens.iter_mut() { for (path, tokens) in all_tokens.iter_mut() {
for token in tokens.drain_filter(|token| texture_names.contains(token.key.fragment())) { for token in tokens.extract_if(|token| texture_names.contains(token.key.fragment())) {
let mut relative_path = path.to_path_buf(); let mut relative_path = path.to_path_buf();
relative_path.push(*token.value.fragment()); relative_path.push(*token.value.fragment());
relative_path relative_path