From b295ff62ef41bdc51d3ad186abc1335ce4f3ac6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Wed, 10 Jul 2024 21:05:26 +0200 Subject: [PATCH] derive: Replace `Rc`s with `Arc`s --- rinja_derive/src/config.rs | 8 ++++---- rinja_derive/src/generator.rs | 6 +++--- rinja_derive/src/heritage.rs | 8 ++++---- rinja_derive/src/input.rs | 16 ++++++++-------- rinja_parser/src/lib.rs | 14 +++++++------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/rinja_derive/src/config.rs b/rinja_derive/src/config.rs index 471557ff..8e3c57bb 100644 --- a/rinja_derive/src/config.rs +++ b/rinja_derive/src/config.rs @@ -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, CompileError> { + ) -> std::result::Result, 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> { pub(crate) fn get_template_source( tpl_path: &Path, - import_from: Option<(&Rc, &str, &str)>, -) -> Result, CompileError> { + import_from: Option<(&Arc, &str, &str)>, +) -> Result, CompileError> { match fs::read_to_string(tpl_path) { Ok(mut source) => { if source.ends_with('\n') { diff --git a/rinja_derive/src/generator.rs b/rinja_derive/src/generator.rs index 0dd56337..bddd9f67 100644 --- a/rinja_derive/src/generator.rs +++ b/rinja_derive/src/generator.rs @@ -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, Context<'a>>, + contexts: &'a HashMap<&'a Arc, 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, Context<'n>>, + contexts: &'n HashMap<&'n Arc, Context<'n>>, heritage: Option<&'n Heritage<'_>>, locals: MapChain<'n, Cow<'n, str>, LocalMeta>, buf_writable_discard: bool, diff --git a/rinja_derive/src/heritage.rs b/rinja_derive/src/heritage.rs index e9f72342..9d064c8e 100644 --- a/rinja_derive/src/heritage.rs +++ b/rinja_derive/src/heritage.rs @@ -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, Context<'n>>, + contexts: &'n HashMap<&'n Arc, 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>, + pub(crate) extends: Option>, 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>, + pub(crate) imports: HashMap<&'a str, Arc>, path: Option<&'a Path>, parsed: &'a Parsed, } diff --git a/rinja_derive/src/input.rs b/rinja_derive/src/input.rs index a86ee0fb..41aad5fc 100644 --- a/rinja_derive/src/input.rs +++ b/rinja_derive/src/input.rs @@ -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, + pub(crate) path: Arc, } impl TemplateInput<'_> { @@ -113,18 +113,18 @@ impl TemplateInput<'_> { pub(crate) fn find_used_templates( &self, - map: &mut HashMap, Parsed>, + map: &mut HashMap, 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| -> Result<(), CompileError> { + let mut add_to_check = |new_path: Arc| -> 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), + Source(Arc), } #[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, Rc)]) -> Result<(), CompileError> { +fn cyclic_graph_error(dependency_graph: &[(Arc, Arc)]) -> Result<(), CompileError> { Err(CompileError::no_file_info(format!( "cyclic dependency in graph {:#?}", dependency_graph diff --git a/rinja_parser/src/lib.rs b/rinja_parser/src/lib.rs index 3e55bacb..2999d0ee 100644 --- a/rinja_parser/src/lib.rs +++ b/rinja_parser/src/lib.rs @@ -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, + source: Arc, } 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, - file_path: Option>, + source: Arc, + file_path: Option>, syntax: &Syntax<'_>, ) -> Result { // 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>, + file_path: Option>, syntax: &Syntax<'_>, ) -> Result { 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>, + file_path: Option>, }, }