logos icon indicating copy to clipboard operation
logos copied to clipboard

Add an ability to use smart pointers for source

Open InfiniteCoder01 opened this issue 1 year ago • 5 comments

This might be useful for #159, #324 and #340. I don't know anything about proc-macros in rust, but I'll try to implement it.

InfiniteCoder01 avatar Oct 15 '23 19:10 InfiniteCoder01

I did not realize that this feature is there, we need docs for proc macros. But it's expecting type to be something that we put behind a reference, which is not the case with Rc or Arc. Changing the title

InfiniteCoder01 avatar Oct 15 '23 19:10 InfiniteCoder01

Hello @InfiniteCoder01, could you provide an example code showcasing a basic use case?

jeertmans avatar Oct 16 '23 07:10 jeertmans

Example of this that I can think of is quite complex, but here it is: When using logos in combination with codespan_reporting and writing a compiler for a language, that is like rust - uses mod somemodule; to mark somemodule.rs or somemodule/mod.rs as files to parse & link. Especially useful with multithreading. You are parsing a file, lexer is a stream of tokens, you encounter mod somemodule;. (Normally you would put it into AST but in my case AST is more like LLVM IR). You need to search for module file and add it to codespan_reporting Files. If those files where borrowed by lexer, it would be impossible to add a file immediately. When using Arc, we can read this file and put it into this database immediately, perhaps even start a separate thread, parsing it. This explanation is quite complex, but I hope it made it clearer.

InfiniteCoder01 avatar Oct 18 '23 19:10 InfiniteCoder01

For now, we can use a workaround:

let source = file.source().clone(); // File is codespan_reporting::files::SimpleFile<String, Rc<str>>
let mut lexer = lexer::Token::lexer(&source);

InfiniteCoder01 avatar Oct 18 '23 19:10 InfiniteCoder01

Can't you just deref Arc<T> to &T? and use &T as the source?

axelkar avatar Jun 28 '24 23:06 axelkar