Closed kavika13 closed 7 months ago
This seems to work around the problem for me, for my use case:
pub trait StringStorageAsBorrowed<'input> {
fn as_borrowed_str(&self) -> &'input str;
}
impl<'input> StringStorageAsBorrowed<'input> for StringStorage<'input> {
fn as_borrowed_str(&self) -> &'input str {
match self {
StringStorage::Borrowed(s) => s,
_ => panic!("The specified StringStorage was an owned string, not a borrowed string"),
}
}
}
// ...
test_borrow_xml.value = node.text_storage().unwrap().as_borrowed_str();
Out of curiosity, is this due to the choice to return &str
on the interface and not COWs?
https://github.com/RazrFalcon/roxmltree/issues/88
I seemed to have better luck with lifetimes with attribute nodes than with node inner text.
You are expected to copy the string or process it in-place (like parsing a number and stuff) or use a callback/closure.
Your code would not work, because you're trying to return a reference to a local/function-scope string. Of course it's not possible and Rust warns you about it.
And the panic!
hack allows you to cut those cases altogether, making borrow checker happy. But you would not be able to parse generic XML files like this.
Out of curiosity, is this due to the choice to return &str on the interface and not COWs?
We cannot return a COW, because it's owned. That wasn't a choice really.
I'm not really very experienced with Rust yet, so forgive me if this is an obvious and trivial use of the borrow checker. I also think it might be a bug in the lifetime definitions of as_str?
I'm wondering why this compiles and works:
...but this causes a compile error that the document is borrowed and dropped?
Here's a full test case that repros the problem:
And here's the resulting error message:
If I swap that commented code in, then it compiles and runs successfully.