derive: Replace Rcs with Arcs

This commit is contained in:
René Kijewski 2024-07-10 21:05:26 +02:00
parent 85330b353a
commit b295ff62ef
5 changed files with 26 additions and 26 deletions

View File

@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use std::{env, fs};
use parser::node::Whitespace;
@ -120,7 +120,7 @@ impl<'a> Config<'a> {
&self,
path: &str,
start_at: Option<&Path>,
) -> std::result::Result<Rc<Path>, CompileError> {
) -> std::result::Result<Arc<Path>, CompileError> {
if let Some(root) = start_at {
let relative = root.with_file_name(path);
if relative.exists() {
@ -294,8 +294,8 @@ fn str_set<'a>(vals: &[&'a str]) -> Vec<Cow<'a, str>> {
pub(crate) fn get_template_source(
tpl_path: &Path,
import_from: Option<(&Rc<Path>, &str, &str)>,
) -> Result<Rc<str>, CompileError> {
import_from: Option<(&Arc<Path>, &str, &str)>,
) -> Result<Arc<str>, CompileError> {
match fs::read_to_string(tpl_path) {
Ok(mut source) => {
if source.ends_with('\n') {

View File

@ -3,7 +3,7 @@ use std::collections::hash_map::{Entry, HashMap};
use std::fmt::{Arguments, Display, Write};
use std::ops::Deref;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::{cmp, hash, mem, str};
use parser::node::{
@ -21,7 +21,7 @@ pub(crate) struct Generator<'a> {
// The template input state: original struct AST and attributes
input: &'a TemplateInput<'a>,
// All contexts, keyed by the package-relative template path
contexts: &'a HashMap<&'a Rc<Path>, Context<'a>>,
contexts: &'a HashMap<&'a Arc<Path>, Context<'a>>,
// The heritage contains references to blocks and their ancestry
heritage: Option<&'a Heritage<'a>>,
// Variables accessible directly from the current scope (not redirected to context)
@ -44,7 +44,7 @@ pub(crate) struct Generator<'a> {
impl<'a> Generator<'a> {
pub(crate) fn new<'n>(
input: &'n TemplateInput<'_>,
contexts: &'n HashMap<&'n Rc<Path>, Context<'n>>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
heritage: Option<&'n Heritage<'_>>,
locals: MapChain<'n, Cow<'n, str>, LocalMeta>,
buf_writable_discard: bool,

View File

@ -1,6 +1,6 @@
use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use parser::node::{BlockDef, Macro};
use parser::{Node, Parsed, WithSpan};
@ -16,7 +16,7 @@ pub(crate) struct Heritage<'a> {
impl Heritage<'_> {
pub(crate) fn new<'n>(
mut ctx: &'n Context<'n>,
contexts: &'n HashMap<&'n Rc<Path>, Context<'n>>,
contexts: &'n HashMap<&'n Arc<Path>, Context<'n>>,
) -> Heritage<'n> {
let mut blocks: BlockAncestry<'n> = ctx
.blocks
@ -40,10 +40,10 @@ type BlockAncestry<'a> = HashMap<&'a str, Vec<(&'a Context<'a>, &'a BlockDef<'a>
#[derive(Clone)]
pub(crate) struct Context<'a> {
pub(crate) nodes: &'a [Node<'a>],
pub(crate) extends: Option<Rc<Path>>,
pub(crate) extends: Option<Arc<Path>>,
pub(crate) blocks: HashMap<&'a str, &'a BlockDef<'a>>,
pub(crate) macros: HashMap<&'a str, &'a Macro<'a>>,
pub(crate) imports: HashMap<&'a str, Rc<Path>>,
pub(crate) imports: HashMap<&'a str, Arc<Path>>,
path: Option<&'a Path>,
parsed: &'a Parsed,
}

View File

@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::collections::hash_map::{Entry, HashMap};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::Arc;
use std::str::FromStr;
use mime::Mime;
@ -22,7 +22,7 @@ pub(crate) struct TemplateInput<'a> {
pub(crate) escaper: &'a str,
pub(crate) ext: Option<&'a str>,
pub(crate) mime_type: String,
pub(crate) path: Rc<Path>,
pub(crate) path: Arc<Path>,
}
impl TemplateInput<'_> {
@ -113,18 +113,18 @@ impl TemplateInput<'_> {
pub(crate) fn find_used_templates(
&self,
map: &mut HashMap<Rc<Path>, Parsed>,
map: &mut HashMap<Arc<Path>, Parsed>,
) -> Result<(), CompileError> {
let (source, source_path) = match &self.source {
Source::Source(s) => (s.clone(), None),
Source::Path(_) => (
get_template_source(&self.path, None)?,
Some(Rc::clone(&self.path)),
Some(Arc::clone(&self.path)),
),
};
let mut dependency_graph = Vec::new();
let mut check = vec![(Rc::clone(&self.path), source, source_path)];
let mut check = vec![(Arc::clone(&self.path), source, source_path)];
while let Some((path, source, source_path)) = check.pop() {
let parsed = Parsed::new(source, source_path, self.syntax)?;
@ -132,7 +132,7 @@ impl TemplateInput<'_> {
let mut nested = vec![parsed.nodes()];
while let Some(nodes) = nested.pop() {
for n in nodes {
let mut add_to_check = |new_path: Rc<Path>| -> Result<(), CompileError> {
let mut add_to_check = |new_path: Arc<Path>| -> Result<(), CompileError> {
if let Entry::Vacant(e) = map.entry(new_path) {
// Add a dummy entry to `map` in order to prevent adding `path`
// multiple times to `check`.
@ -422,7 +422,7 @@ fn extension(path: &Path) -> Option<&str> {
#[derive(Debug, Hash, PartialEq)]
pub(crate) enum Source {
Path(String),
Source(Rc<str>),
Source(Arc<str>),
}
#[derive(Clone, Copy, Debug, PartialEq, Hash)]
@ -483,7 +483,7 @@ const TEXT_TYPES: [(Mime, Mime); 7] = [
(mime::IMAGE_SVG, mime::IMAGE_SVG),
];
fn cyclic_graph_error(dependency_graph: &[(Rc<Path>, Rc<Path>)]) -> Result<(), CompileError> {
fn cyclic_graph_error(dependency_graph: &[(Arc<Path>, Arc<Path>)]) -> Result<(), CompileError> {
Err(CompileError::no_file_info(format!(
"cyclic dependency in graph {:#?}",
dependency_graph

View File

@ -6,7 +6,7 @@ use std::cell::Cell;
use std::env::current_dir;
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, str};
use nom::branch::alt;
@ -30,7 +30,7 @@ mod tests;
mod _parsed {
use std::path::Path;
use std::rc::Rc;
use std::sync::Arc;
use std::{fmt, mem};
use super::node::Node;
@ -40,15 +40,15 @@ mod _parsed {
// `source` must outlive `ast`, so `ast` must be declared before `source`
ast: Ast<'static>,
#[allow(dead_code)]
source: Rc<str>,
source: Arc<str>,
}
impl Parsed {
/// If `file_path` is `None`, it means the `source` is an inline template. Therefore, if
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
pub fn new(
source: Rc<str>,
file_path: Option<Rc<Path>>,
source: Arc<str>,
file_path: Option<Arc<Path>>,
syntax: &Syntax<'_>,
) -> Result<Self, ParseError> {
// Self-referential borrowing: `self` will keep the source alive as `String`,
@ -105,7 +105,7 @@ impl<'a> Ast<'a> {
/// a parsing error occurs, we won't display the path as it wouldn't be useful.
pub fn from_str(
src: &'a str,
file_path: Option<Rc<Path>>,
file_path: Option<Arc<Path>>,
syntax: &Syntax<'_>,
) -> Result<Self, ParseError> {
let parse = |i: &'a str| Node::many(i, &State::new(syntax));
@ -204,7 +204,7 @@ pub enum ParseError {
row: usize,
column: usize,
source_after: String,
file_path: Option<Rc<Path>>,
file_path: Option<Arc<Path>>,
},
}