Skip to content

Commit 7a07ea0

Browse files
committed
add CompileResult::try_map and map
1 parent 5630f49 commit 7a07ea0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

crates/rustc_codegen_spirv-types/src/compile_result.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use serde::{Deserialize, Serialize};
22
use std::collections::BTreeMap;
3+
use std::convert::Infallible;
34
use std::fmt::Write;
45
use std::path::PathBuf;
56

@@ -41,6 +42,42 @@ pub struct GenericCompileResult<T> {
4142
}
4243

4344
impl<T> GenericCompileResult<T> {
45+
pub fn try_map<R, E>(
46+
&self,
47+
mut map_entry_point: impl FnMut(&String) -> Result<String, E>,
48+
mut map_module: impl FnMut(&T) -> Result<R, E>,
49+
) -> Result<GenericCompileResult<R>, E> {
50+
Ok(match &self.module {
51+
GenericModuleResult::SingleModule(t) => GenericCompileResult {
52+
entry_points: self
53+
.entry_points
54+
.iter()
55+
.map(map_entry_point)
56+
.collect::<Result<_, _>>()?,
57+
module: GenericModuleResult::SingleModule(map_module(t)?),
58+
},
59+
GenericModuleResult::MultiModule(map) => {
60+
let new_map: BTreeMap<String, R> = map
61+
.iter()
62+
.map(|(entry_point, t)| Ok((map_entry_point(entry_point)?, map_module(t)?)))
63+
.collect::<Result<_, _>>()?;
64+
GenericCompileResult {
65+
entry_points: new_map.keys().cloned().collect(),
66+
module: GenericModuleResult::MultiModule(new_map),
67+
}
68+
}
69+
})
70+
}
71+
72+
pub fn map<R>(
73+
&self,
74+
mut map_entry_point: impl FnMut(&String) -> String,
75+
mut map_module: impl FnMut(&T) -> R,
76+
) -> GenericCompileResult<R> {
77+
self.try_map::<_, Infallible>(|e| Ok(map_entry_point(e)), |e| Ok(map_module(e)))
78+
.unwrap()
79+
}
80+
4481
pub fn codegen_entry_point_strings(&self) -> String {
4582
let trie = Trie::create_from(self.entry_points.iter().map(|x| x as &str));
4683
let mut builder = String::new();

0 commit comments

Comments
 (0)