Skip to content

Add method to provide access to the refdef tracker - #512

Merged
marcusklaas merged 2 commits into
pulldown-cmark:masterfrom
marcusklaas:refdefs
Jan 5, 2021
Merged

marcusklaas merged 2 commits into
pulldown-cmark:masterfrom
marcusklaas:refdefs

Conversation

@marcusklaas

Copy link
Copy Markdown
Collaborator

Currently there is no way for users to get information about reference definitions. So far, the following use cases have been identified for it:

  • recreating markdown documents (e.g. markdown-to-markdown formatting)
  • linting on unused definitions
  • reporting errors at link definition (for rustdoc)

Instead of emitting these definitions as events, we provide a separate call for these. These were the considerations:

  • not all renderers will be interested in refdefs, so we should provide them as opt-in to prevent unneeded complexity
  • the current exposed structure provides quick lookups, as the underlying datastructure is a hashmap

This new functionality will only really start to shine when link references labels are exposed (#434). That is not included in this pull request for two reasons: it will be a breaking change, and the ideal design for exposing reference labels is not yet clear. Simply adding it to the link event (type) will blow up the size of all items by 3 words, which will have non-negligible impact on performance.

Until then the reference label can be recovered in most cases by slicing the source document using the link's span (parsing up to the first ]).

Closes #423.

Comment thread src/parse.rs
Comment on lines +1217 to +1218
where
's: 'b,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this bound necessary? I'm pretty sure it will make the output of get() unusable since it can't be used after the self borrow is released.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that it is necessary. And I think that it means something different: that the output of get cannot outlive self (not the borrow of self), which is indeed required for memory safety.

To confirm this, I added let linkdef = parser.reference_definitions().get("a b c").unwrap(); to the test case and it compiles. That seems to confirm that the linkdef reference can indeed outlive the RefDefs reference.

@jyn514 jyn514 Jan 2, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this:

diff --git a/src/parse.rs b/src/parse.rs
index 35e9647..6982059 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -1203,7 +1203,7 @@ pub(crate) struct AlignmentIndex(usize);
 
 #[derive(Clone)]
 pub(crate) struct Allocations<'a> {
-    pub refdefs: RefDefs<'a>,
+    pub refdefs: RefDefs<'a, 'a>,
     links: Vec<(LinkType, CowStr<'a>, CowStr<'a>)>,
     cows: Vec<CowStr<'a>>,
     alignments: Vec<Vec<Alignment>>,
@@ -1211,19 +1211,16 @@ pub(crate) struct Allocations<'a> {
 
 /// Keeps track of the reference definitions defined in the document.
 #[derive(Clone)]
-pub struct RefDefs<'input>(pub(crate) HashMap<LinkLabel<'input>, LinkDef<'input>>);
+pub struct RefDefs<'input, 'output>(pub(crate) HashMap<LinkLabel<'input>, LinkDef<'output>>);
 
-impl<'input, 'b, 's> RefDefs<'input>
-where
-    's: 'b,
-{
+impl<'input, 'output, 's> RefDefs<'input, 'output> {
     /// Performs a lookup on reference label using unicode case folding.
-    pub fn get(&'s self, key: &'b str) -> Option<&'b LinkDef<'input>> {
+    pub fn get(&'s self, key: &'input str) -> Option<&'s LinkDef<'output>> {
         self.0.get(&UniCase::new(key.into()))
     }
 
     /// Provides an iterator over all the document's reference definitions.
-    pub fn iter(&'s self) -> impl Iterator<Item = (&'s str, &'s LinkDef<'input>)> {
+    pub fn iter(&'s self) -> impl Iterator<Item = (&'s str, &'s LinkDef<'output>)> {
         self.0.iter().map(|(k, v)| (k.as_ref(), v))
     }
 }

That

  • Decouples the lifetime of LinkLabel from LinkDef
  • Returns a LinkDef that can live longer than the key

I think previously this case would break:

let parser = unimplemented!();
let def = {
    let key = String::from("hello");
    parser.allocs.refdefs.get(&key)
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said my example is sort of convoluted and it's probably ok to just not fix this.

@marcusklaas marcusklaas Jan 4, 2021 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, indeed that case breaks. But even with your patch, I cannot get it to compile. From my understanding, it still demands that they key lives as long as the hashmap keys.

It should obviously be possible to do this. I believe that LinkLabel<'a> should implement Borrow<LinkLabel<'b>> where 'a: 'b to enable it. edit: oh, it already does. I'm lost now. 🤔

I have a lot more confidence in your lifetime analysis expertise, so please let me know when I'm missing something. I'll keep tinkering in the meantime to try to get your case to compile.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I did more digging and asked for help on the xi zulip chat here. It seems that this is a limitation of the Hashmap API (and ultimately Rust itself).

@jyn514

jyn514 commented Jan 2, 2021

Copy link
Copy Markdown
Contributor

It would be great to see an example of how to use this :) both as documentation and as a test for things like #512 (comment).

@raphlinus raphlinus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this looks good. The lifetimes are slightly scary, but hopefully won't be a problem in real use.

Comment thread src/parse.rs Outdated
pub fn new() -> Self {
Self {
refdefs: HashMap::new(),
refdefs: RefDefs(HashMap::new()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I prefer to #[derive(Default)] on the type and use default here, but that's a style point.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's cleaner. I'll make the change.

@marcusklaas
marcusklaas merged commit 9565f52 into pulldown-cmark:master Jan 5, 2021
@marcusklaas
marcusklaas deleted the refdefs branch January 5, 2021 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve link reference definitions in parser output

3 participants