Skip to content

Generic compile result #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 55 additions & 17 deletions crates/rustc_codegen_spirv-types/src/compile_result.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,83 @@
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::convert::Infallible;
use std::fmt::Write;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

pub type ModuleResult = GenericModuleResult<PathBuf>;

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ModuleResult {
SingleModule(PathBuf),
MultiModule(BTreeMap<String, PathBuf>),
pub enum GenericModuleResult<T> {
SingleModule(T),
MultiModule(BTreeMap<String, T>),
}

impl ModuleResult {
pub fn unwrap_single(&self) -> &Path {
impl<T> GenericModuleResult<T> {
pub fn unwrap_single(&self) -> &T {
match self {
ModuleResult::SingleModule(result) => result,
ModuleResult::MultiModule(_) => {
GenericModuleResult::SingleModule(result) => result,
GenericModuleResult::MultiModule(_) => {
panic!("called `ModuleResult::unwrap_single()` on a `MultiModule` result")
}
}
}

pub fn unwrap_multi(&self) -> &BTreeMap<String, PathBuf> {
pub fn unwrap_multi(&self) -> &BTreeMap<String, T> {
match self {
ModuleResult::MultiModule(result) => result,
ModuleResult::SingleModule(_) => {
GenericModuleResult::MultiModule(result) => result,
GenericModuleResult::SingleModule(_) => {
panic!("called `ModuleResult::unwrap_multi()` on a `SingleModule` result")
}
}
}
}

pub type CompileResult = GenericCompileResult<PathBuf>;

#[derive(Debug, Serialize, Deserialize)]
pub struct CompileResult {
pub struct GenericCompileResult<T> {
pub entry_points: Vec<String>,
pub module: ModuleResult,
pub module: GenericModuleResult<T>,
}

impl CompileResult {
impl<T> GenericCompileResult<T> {
pub fn try_map<R, E>(
&self,
mut map_entry_point: impl FnMut(&String) -> Result<String, E>,
mut map_module: impl FnMut(&T) -> Result<R, E>,
) -> Result<GenericCompileResult<R>, E> {
Ok(match &self.module {
GenericModuleResult::SingleModule(t) => GenericCompileResult {
entry_points: self
.entry_points
.iter()
.map(map_entry_point)
.collect::<Result<_, _>>()?,
module: GenericModuleResult::SingleModule(map_module(t)?),
},
GenericModuleResult::MultiModule(map) => {
let new_map: BTreeMap<String, R> = map
.iter()
.map(|(entry_point, t)| Ok((map_entry_point(entry_point)?, map_module(t)?)))
.collect::<Result<_, _>>()?;
GenericCompileResult {
entry_points: new_map.keys().cloned().collect(),
module: GenericModuleResult::MultiModule(new_map),
}
}
})
}

pub fn map<R>(
&self,
mut map_entry_point: impl FnMut(&String) -> String,
mut map_module: impl FnMut(&T) -> R,
) -> GenericCompileResult<R> {
self.try_map::<_, Infallible>(|e| Ok(map_entry_point(e)), |e| Ok(map_module(e)))
.unwrap()
}

pub fn codegen_entry_point_strings(&self) -> String {
let trie = Trie::create_from(self.entry_points.iter().map(|x| x as &str));
let mut builder = String::new();
Expand Down Expand Up @@ -110,9 +151,6 @@ impl<'a> Trie<'a> {
}
}

#[allow(non_upper_case_globals)]
pub const a: &str = "x::a";

#[cfg(test)]
mod test {
use super::*;
Expand Down
3 changes: 1 addition & 2 deletions crates/spirv-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use thiserror::Error;

pub use rustc_codegen_spirv_types::Capability;
pub use rustc_codegen_spirv_types::{CompileResult, ModuleResult};
pub use rustc_codegen_spirv_types::*;

#[cfg(feature = "include-target-specs")]
pub use rustc_codegen_spirv_target_specs::TARGET_SPEC_DIR_PATH;
Expand Down