potassco/clingo-rs

Return `&'static str` instead of `&str` in `Symbol` functions

peacememories opened this issue · 4 comments

As far as I can tell, clingo internally does not free allocated strings ( taken from here ). On one hand this can be an issue with long running applications, on the other hand this would mean all functions in this library that return string slices could correctly return &static str.
Besides being correct this would have the benefit of allowing consumers of the library to store the slices without first allocating a string.

PS:

This has the additional benefit of stating up front what the memory behavior of the library is going to be. Because strings were bound to the lifetime of symbols, which themselves were only ids, my first worry was that there would be a soundness bug somewhere 😅

This does not hold for all strings (but most). Strings associated with symbols are internalized. Other strings might be but it is better not to make the assumption. For example it is better to assume that the lifetime of strings in the AST (the strings here are internalized) or TheoryData (the strings here are not internalized because they come from clasp) is tied to the lifetime of the corresponding owning objects. Just drop me a message for cases were you are not sure or need more info.

Yes, you're correct in that this doesn't hold for all &strs. The two functions where I'm certain that it is the case are Symbol::string and Symbol::name, and those are also the ones most interesting for me, as I'm writing conversion logic in the form of &Symbol -> T (slightly related to potassco/clingo_derive#3 ) :)

Ah, I completely missed the second part of that conversation in potassco/clingo#186
I guess my comment can be disregarded 😅

commit f823e0c to wip branch
The following functions now return &'static str for internalized strings.

Location::begin_file()
Location::end_file()
Signature::name()
Symbol::name()
Symbol::string()