ocl_include/source/
container.rs1use super::Source;
2use std::{
3 io,
4 path::{Path, PathBuf},
5 rc::Rc,
6};
7
8impl<S: Source> Source for Vec<S> {
10 fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
11 let mut res = None;
12 for source in self.iter() {
13 match source.read(path, dir) {
14 Ok(x) => {
15 res = Some(Ok(x));
16 break;
17 }
18 Err(e) => match e.kind() {
19 io::ErrorKind::NotFound => continue,
20 _ => {
21 res = Some(Err(e));
22 break;
23 }
24 },
25 }
26 }
27
28 match res {
29 Some(x) => x,
30 None => Err(io::Error::new(
31 io::ErrorKind::NotFound,
32 format!("path: {:?}, dir: {:?}", path, dir),
33 )),
34 }
35 }
36}
37
38#[macro_export]
39macro_rules! source_vec {
40 ($($x:expr),* $(,)?) => {
41 vec![ $(Box::new($x) as Box::<dyn $crate::Source>),* ]
42 };
43}
44
45impl<'a, S: Source> Source for &'a S {
47 fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
48 (*self).read(path, dir)
49 }
50}
51
52impl Source for Box<dyn Source> {
54 fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
55 self.as_ref().read(path, dir)
56 }
57}
58
59impl<S: Source> Source for Rc<S> {
61 fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
62 self.as_ref().read(path, dir)
63 }
64}