presets: use crate for extract_if with fully qualified syntax to avoid name collision with unstable std

This commit is contained in:
chyyran 2024-08-20 22:08:17 -04:00 committed by Ronny Chan
parent 1957f576ca
commit db3d5d05a6
7 changed files with 61 additions and 361 deletions

7
Cargo.lock generated
View file

@ -1706,6 +1706,7 @@ dependencies = [
"once_cell", "once_cell",
"regex", "regex",
"thiserror", "thiserror",
"vec_extract_if_polyfill",
] ]
[[package]] [[package]]
@ -3418,6 +3419,12 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "vec_extract_if_polyfill"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c9cb5fb67c2692310b6eb3fce7dd4b6e4c9a75be4f2f46b27f0b2b7799759c"
[[package]] [[package]]
name = "version-compare" name = "version-compare"
version = "0.2.0" version = "0.2.0"

View file

@ -20,6 +20,7 @@ num-traits = "0.2"
once_cell = "1" once_cell = "1"
# we don't need unicode # we don't need unicode
regex = { version = "1", default-features = false, features = ["perf"] } regex = { version = "1", default-features = false, features = ["perf"] }
vec_extract_if_polyfill = "0.1.0"
[features] [features]
parse_legacy_glsl = [] parse_legacy_glsl = []

View file

@ -1,312 +0,0 @@
//! This is a stable polyfill for [`Vec::extract_if`](https://github.com/rust-lang/rust/issues/43244).
use core::ptr;
use core::slice;
/// Polyfill trait for [`Vec::extract_if`](https://github.com/rust-lang/rust/issues/43244).
pub(crate) trait MakeExtractIf<T> {
/// Creates an iterator which uses a closure to determine if an element should be removed.
///
/// If the closure returns true, then the element is removed and yielded.
/// If the closure returns false, the element will remain in the vector and will not be yielded
/// by the iterator.
///
/// If the returned `ExtractIf` is not exhausted, e.g. because it is dropped without iterating
/// or the iteration short-circuits, then the remaining elements will be retained.
///
/// Note that `extract_if` also lets you mutate every element in the filter closure,
/// regardless of whether you choose to keep or remove it.
///
/// # Examples
///
/// Splitting an array into evens and odds, reusing the original allocation:
///
/// ```
/// use vec_extract_if_polyfill::MakeExtractIf;
/// let mut numbers = vec![1, 2, 3, 4, 5, 6, 8, 9, 11, 13, 14, 15];
///
/// let evens = numbers.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
/// let odds = numbers;
///
/// assert_eq!(evens, vec![2, 4, 6, 8, 14]);
/// assert_eq!(odds, vec![1, 3, 5, 9, 11, 13, 15]);
/// ```
fn extract_if<F>(&mut self, filter: F) -> ExtractIf<T, F>
where
F: FnMut(&mut T) -> bool;
}
impl<T> MakeExtractIf<T> for Vec<T> {
fn extract_if<F>(&mut self, filter: F) -> ExtractIf<T, F>
where
F: FnMut(&mut T) -> bool,
{
let old_len = self.len();
// Guard against us getting leaked (leak amplification)
unsafe {
self.set_len(0);
}
ExtractIf {
vec: self,
idx: 0,
del: 0,
old_len,
pred: filter,
}
}
}
/// An iterator which uses a closure to determine if an element should be removed.
///
/// This struct is created by [`Vec::extract_if`].
/// See its documentation for more.
///
/// # Example
///
/// ```
/// use vec_extract_if_polyfill::MakeExtractIf;
///
/// let mut v = vec![0, 1, 2];
/// let iter = v.extract_if(|x| *x % 2 == 0);
/// ```
#[derive(Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct ExtractIf<'a, T, F>
where
F: FnMut(&mut T) -> bool,
{
vec: &'a mut Vec<T>,
/// The index of the item that will be inspected by the next call to `next`.
idx: usize,
/// The number of items that have been drained (removed) thus far.
del: usize,
/// The original length of `vec` prior to draining.
old_len: usize,
/// The filter test predicate.
pred: F,
}
impl<T, F> Iterator for ExtractIf<'_, T, F>
where
F: FnMut(&mut T) -> bool,
{
type Item = T;
fn next(&mut self) -> Option<T> {
unsafe {
while self.idx < self.old_len {
let i = self.idx;
let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len);
let drained = (self.pred)(&mut v[i]);
// Update the index *after* the predicate is called. If the index
// is updated prior and the predicate panics, the element at this
// index would be leaked.
self.idx += 1;
if drained {
self.del += 1;
return Some(ptr::read(&v[i]));
} else if self.del > 0 {
let del = self.del;
let src: *const T = &v[i];
let dst: *mut T = &mut v[i - del];
ptr::copy_nonoverlapping(src, dst, 1);
}
}
None
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(0, Some(self.old_len - self.idx))
}
}
impl<T, F> Drop for ExtractIf<'_, T, F>
where
F: FnMut(&mut T) -> bool,
{
fn drop(&mut self) {
unsafe {
if self.idx < self.old_len && self.del > 0 {
// This is a pretty messed up state, and there isn't really an
// obviously right thing to do. We don't want to keep trying
// to execute `pred`, so we just backshift all the unprocessed
// elements and tell the vec that they still exist. The backshift
// is required to prevent a double-drop of the last successfully
// drained item prior to a panic in the predicate.
let ptr = self.vec.as_mut_ptr();
let src = ptr.add(self.idx);
let dst = src.sub(self.del);
let tail_len = self.old_len - self.idx;
src.copy_to(dst, tail_len);
}
self.vec.set_len(self.old_len - self.del);
}
}
}
#[cfg(test)]
mod test {
use crate::extract_if::MakeExtractIf;
#[test]
fn drain_filter_empty() {
let mut vec: Vec<i32> = vec![];
{
let mut iter = vec.extract_if(|_| true);
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
assert_eq!(iter.size_hint(), (0, Some(0)));
}
assert_eq!(vec.len(), 0);
assert_eq!(vec, vec![]);
}
#[test]
fn drain_filter_zst() {
let mut vec = vec![(), (), (), (), ()];
let initial_len = vec.len();
let mut count = 0;
{
let mut iter = vec.extract_if(|_| true);
assert_eq!(iter.size_hint(), (0, Some(initial_len)));
while let Some(_) = iter.next() {
count += 1;
assert_eq!(iter.size_hint(), (0, Some(initial_len - count)));
}
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
assert_eq!(iter.size_hint(), (0, Some(0)));
}
assert_eq!(count, initial_len);
assert_eq!(vec.len(), 0);
assert_eq!(vec, vec![]);
}
#[test]
fn drain_filter_false() {
let mut vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let initial_len = vec.len();
let mut count = 0;
{
let mut iter = vec.extract_if(|_| false);
assert_eq!(iter.size_hint(), (0, Some(initial_len)));
for _ in iter.by_ref() {
count += 1;
}
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
assert_eq!(iter.size_hint(), (0, Some(0)));
}
assert_eq!(count, 0);
assert_eq!(vec.len(), initial_len);
assert_eq!(vec, vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}
#[test]
fn drain_filter_true() {
let mut vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let initial_len = vec.len();
let mut count = 0;
{
let mut iter = vec.extract_if(|_| true);
assert_eq!(iter.size_hint(), (0, Some(initial_len)));
while let Some(_) = iter.next() {
count += 1;
assert_eq!(iter.size_hint(), (0, Some(initial_len - count)));
}
assert_eq!(iter.size_hint(), (0, Some(0)));
assert_eq!(iter.next(), None);
assert_eq!(iter.size_hint(), (0, Some(0)));
}
assert_eq!(count, initial_len);
assert_eq!(vec.len(), 0);
assert_eq!(vec, vec![]);
}
#[test]
fn drain_filter_complex() {
{
// [+xxx++++++xxxxx++++x+x++]
let mut vec = vec![
1, 2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36,
37, 39,
];
let removed = vec.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(removed, vec![2, 4, 6, 18, 20, 22, 24, 26, 34, 36]);
assert_eq!(vec.len(), 14);
assert_eq!(
vec,
vec![1, 7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35, 37, 39]
);
}
{
// [xxx++++++xxxxx++++x+x++]
let mut vec = vec![
2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36, 37,
39,
];
let removed = vec.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(removed, vec![2, 4, 6, 18, 20, 22, 24, 26, 34, 36]);
assert_eq!(vec.len(), 13);
assert_eq!(vec, vec![7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35, 37, 39]);
}
{
// [xxx++++++xxxxx++++x+x]
let mut vec = vec![
2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36,
];
let removed = vec.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(removed, vec![2, 4, 6, 18, 20, 22, 24, 26, 34, 36]);
assert_eq!(vec.len(), 11);
assert_eq!(vec, vec![7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35]);
}
{
// [xxxxxxxxxx+++++++++++]
let mut vec = vec![
2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
];
let removed = vec.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(removed, vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
assert_eq!(vec.len(), 10);
assert_eq!(vec, vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19]);
}
{
// [+++++++++++xxxxxxxxxx]
let mut vec = vec![
1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
];
let removed = vec.extract_if(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(removed, vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20]);
assert_eq!(vec.len(), 10);
assert_eq!(vec, vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19]);
}
}
}

View file

@ -8,12 +8,8 @@
//! //!
//! 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).
#![allow(stable_features)]
#![allow(unstable_name_collisions)]
pub mod context; pub mod context;
mod error; mod error;
mod extract_if;
mod parse; mod parse;
mod preset; mod preset;

View file

@ -1,11 +1,11 @@
use crate::extract_if::MakeExtractIf;
use crate::parse::remove_if; use crate::parse::remove_if;
use crate::parse::value::Value; use crate::parse::value::Value;
use crate::{ParameterConfig, Scale2D, Scaling, ShaderPassConfig, ShaderPreset, TextureConfig}; use crate::{ParameterConfig, Scale2D, Scaling, ShaderPassConfig, ShaderPreset, TextureConfig};
use vec_extract_if_polyfill::MakeExtractIf;
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> =
.extract_if(|f| matches!(*f, Value::Texture { .. })) MakeExtractIf::extract_if(&mut values, |f| matches!(*f, Value::Texture { .. }))
.map(|value| { .map(|value| {
if let Value::Texture { if let Value::Texture {
name, name,
@ -27,8 +27,8 @@ pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
} }
}) })
.collect(); .collect();
let parameters: Vec<ParameterConfig> = values let parameters: Vec<ParameterConfig> =
.extract_if(|f| matches!(*f, Value::Parameter { .. })) MakeExtractIf::extract_if(&mut values, |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,8 +64,8 @@ pub fn resolve_values(mut values: Vec<Value>) -> ShaderPreset {
&mut values, &mut values,
|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> =
.extract_if(|v| v.shader_index() == Some(shader)) MakeExtractIf::extract_if(&mut values, |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

@ -18,7 +18,7 @@ use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use crate::context::{apply_context, WildcardContext}; use crate::context::{apply_context, WildcardContext};
use crate::extract_if::MakeExtractIf; use vec_extract_if_polyfill::MakeExtractIf;
#[derive(Debug)] #[derive(Debug)]
pub enum Value { pub enum Value {
@ -195,8 +195,10 @@ fn load_child_reference_strings(
.map_err(|e| ParsePresetError::IOError(path.clone(), e))?; .map_err(|e| ParsePresetError::IOError(path.clone(), e))?;
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> =
.extract_if(|token| *token.key.fragment() == "#reference") MakeExtractIf::extract_if(&mut new_tokens, |token| {
*token.key.fragment() == "#reference"
})
.map(|value| PathBuf::from(*value.value.fragment())) .map(|value| PathBuf::from(*value.value.fragment()))
.collect(); .collect();
@ -250,8 +252,8 @@ pub fn parse_values(
root_path.pop(); root_path.pop();
} }
let references: Vec<PathBuf> = tokens let references: Vec<PathBuf> =
.extract_if(|token| *token.key.fragment() == "#reference") MakeExtractIf::extract_if(&mut tokens, |token| *token.key.fragment() == "#reference")
.map(|value| PathBuf::from(*value.value.fragment())) .map(|value| PathBuf::from(*value.value.fragment()))
.collect(); .collect();
@ -275,7 +277,9 @@ 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.extract_if(|token| *token.key.fragment() == "parameters") { for token in
MakeExtractIf::extract_if(tokens, |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);
@ -286,7 +290,8 @@ 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.extract_if(|token| *token.key.fragment() == "textures") { for token in MakeExtractIf::extract_if(tokens, |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);
@ -297,7 +302,9 @@ 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.extract_if(|token| parse_indexed_key("shader", token.key).is_ok()) { for token in MakeExtractIf::extract_if(tokens, |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;
@ -328,7 +335,9 @@ 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.extract_if(|token| texture_names.contains(token.key.fragment())) { for token in
MakeExtractIf::extract_if(tokens, |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

View file

@ -32,7 +32,6 @@ pub struct SpirvCompilation {
pub(crate) fragment: Vec<u32>, pub(crate) fragment: Vec<u32>,
} }
impl TryFrom<&ShaderSource> for SpirvCompilation { impl TryFrom<&ShaderSource> for SpirvCompilation {
type Error = ShaderCompileError; type Error = ShaderCompileError;