Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Modify the self-referencing Parsed struct to use the ouroboros crate. #1127

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions askama_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rust-version = "1.65"

[dependencies]
nom = { version = "7", default-features = false, features = ["alloc"] }
ouroboros = "0.18.5"

[dev-dependencies]
criterion = "0.5"
Expand Down
48 changes: 32 additions & 16 deletions askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,36 @@ pub use node::Node;
mod tests;

mod _parsed {
use std::fmt;
use std::path::Path;
use std::rc::Rc;
use std::{fmt, mem};

use ouroboros::self_referencing;

use super::node::Node;
use super::{Ast, ParseError, Syntax};

#[derive(Default)]
pub struct Parsed {
// `source` must outlive `ast`, so `ast` must be declared before `source`
ast: Ast<'static>,
#[allow(dead_code)]
#[self_referencing]
struct ParsedInternal {
source: String,
#[covariant]
#[borrows(source)]
ast: Ast<'this>,
}

pub struct Parsed {
internal: ParsedInternal,
}

impl Default for Parsed {
fn default() -> Self {
let internal = ParsedInternalBuilder {
source: Default::default(),
ast_builder: |_| Default::default(),
}
.build();
Parsed { internal }
}
}

impl Parsed {
Expand All @@ -48,31 +65,30 @@ mod _parsed {
file_path: Option<Rc<Path>>,
syntax: &Syntax<'_>,
) -> Result<Self, ParseError> {
// Self-referential borrowing: `self` will keep the source alive as `String`,
// internally we will transmute it to `&'static str` to satisfy the compiler.
// However, we only expose the nodes with a lifetime limited to `self`.
let src = unsafe { mem::transmute::<&str, &'static str>(source.as_str()) };
let ast = Ast::from_str(src, file_path, syntax)?;
Ok(Self { ast, source })
let internal = ParsedInternalTryBuilder {
source,
ast_builder: |src: &String| Ast::from_str(src, file_path, syntax),
}
.try_build()?;
Ok(Self { internal })
}

// The return value's lifetime must be limited to `self` to uphold the unsafe invariant.
pub fn nodes(&self) -> &[Node<'_>] {
&self.ast.nodes
&self.internal.borrow_ast().nodes
}
}

impl fmt::Debug for Parsed {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Parsed")
.field("nodes", &self.ast.nodes)
.field("nodes", &self.internal.borrow_ast().nodes)
.finish_non_exhaustive()
}
}

impl PartialEq for Parsed {
fn eq(&self, other: &Self) -> bool {
self.ast.nodes == other.ast.nodes
self.internal.borrow_ast().nodes == other.internal.borrow_ast().nodes
}
}
}
Expand Down