Repository navigation
Add method to provide access to the refdef tracker - #512
Conversation
| where | ||
| 's: 'b, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
LinkLabelfromLinkDef - Returns a
LinkDefthat can live longer than thekey
I think previously this case would break:
let parser = unimplemented!();
let def = {
let key = String::from("hello");
parser.allocs.refdefs.get(&key)
};There was a problem hiding this comment.
That said my example is sort of convoluted and it's probably ok to just not fix this.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
|
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
left a comment
There was a problem hiding this comment.
Sure, this looks good. The lifetimes are slightly scary, but hopefully won't be a problem in real use.
| pub fn new() -> Self { | ||
| Self { | ||
| refdefs: HashMap::new(), | ||
| refdefs: RefDefs(HashMap::new()), |
There was a problem hiding this comment.
Generally I prefer to #[derive(Default)] on the type and use default here, but that's a style point.
There was a problem hiding this comment.
Yeah, that's cleaner. I'll make the change.
Currently there is no way for users to get information about reference definitions. So far, the following use cases have been identified for it:
Instead of emitting these definitions as events, we provide a separate call for these. These were the considerations:
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.