{"action":"created","issue":{"url":"https://api.github.com/repos/trishume/syntect/issues/29","repository_url":"https://api.github.com/repos/trishume/syntect","labels_url":"https://api.github.com/repos/trishume/syntect/issues/29/labels{/name}","comments_url":"https://api.github.com/repos/trishume/syntect/issues/29/comments","events_url":"https://api.github.com/repos/trishume/syntect/issues/29/events","html_url":"https://github.com/trishume/syntect/issues/29","id":198229024,"number":29,"title":"Use in threaded context seems to require unsafe wrapper struct","user":{"login":"ojensen5115","id":2729228,"avatar_url":"https://avatars.githubusercontent.com/u/2729228?v=3","gravatar_id":"","url":"https://api.github.com/users/ojensen5115","html_url":"https://github.com/ojensen5115","followers_url":"https://api.github.com/users/ojensen5115/followers","following_url":"https://api.github.com/users/ojensen5115/following{/other_user}","gists_url":"https://api.github.com/users/ojensen5115/gists{/gist_id}","starred_url":"https://api.github.com/users/ojensen5115/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ojensen5115/subscriptions","organizations_url":"https://api.github.com/users/ojensen5115/orgs","repos_url":"https://api.github.com/users/ojensen5115/repos","events_url":"https://api.github.com/users/ojensen5115/events{/privacy}","received_events_url":"https://api.github.com/users/ojensen5115/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-31T11:33:00Z","updated_at":"2016-12-31T16:18:15Z","closed_at":null,"body":"Documentation states that the SyntaxSet and ThemeSet should be loaded once at program startup (and indeed reloading them whenever highlighting is needed hurts performance). However, SyntaxSet does not appear to be threadsafe, possibly due to the internal use of RefCell in SyntaxDefinition.\r\n\r\nAs a result, the simplest way of getting it to work in Iron that I have found requires the following (unsafe) dance:\r\n\r\n```\r\nstruct HighlighterSyntaxSet {\r\n    ss: SyntaxSet,\r\n}\r\nunsafe impl Sync for HighlighterSyntaxSet {}\r\n\r\nlazy_static! {\r\n    static ref HL_SYNTAX_SET: HighlighterSyntaxSet = {\r\n        let ss = SyntaxSet::load_defaults_newlines();\r\n        HighlighterSyntaxSet {ss: ss}\r\n    };\r\n}\r\n```\r\n\r\nUsing it directly without the unsafe wrapper results in the following error:\r\n\r\n```\r\nthe trait `std::marker::Sync` is not implemented for `std::rc::Rc<std::cell::RefCell<syntect::parsing::syntax_definition::Context>>`\r\n    = note: `std::rc::Rc<std::cell::RefCell<syntect::parsing::syntax_definition::Context>>` cannot be shared between threads safely\r\n    = note: required because it appears within the type `std::option::Option<std::rc::Rc<std::cell::RefCell<syntect::parsing::syntax_definition::Context>>>`\r\n    = note: required because it appears within the type `syntect::parsing::SyntaxDefinition`\r\n    = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<syntect::parsing::SyntaxDefinition>`\r\n    = note: required because it appears within the type `alloc::raw_vec::RawVec<syntect::parsing::SyntaxDefinition>`\r\n    = note: required because it appears within the type `std::vec::Vec<syntect::parsing::SyntaxDefinition>`\r\n    = note: required because it appears within the type `syntect::parsing::SyntaxSet`\r\n\r\n```\r\n\r\nThe use of lazy_static here is simply to avoid the more complex code necessary for using the `persistent` crate, or `Arc`s directly. I don't believe it affects the issue at hand.\r\n\r\nAll that said, it is of course entirely possible I'm using this incorrectly -- I'm somewhat new to Rust."},"comment":{"url":"https://api.github.com/repos/trishume/syntect/issues/comments/269871702","html_url":"https://github.com/trishume/syntect/issues/29#issuecomment-269871702","issue_url":"https://api.github.com/repos/trishume/syntect/issues/29","id":269871702,"user":{"login":"trishume","id":887610,"avatar_url":"https://avatars.githubusercontent.com/u/887610?v=3","gravatar_id":"","url":"https://api.github.com/users/trishume","html_url":"https://github.com/trishume","followers_url":"https://api.github.com/users/trishume/followers","following_url":"https://api.github.com/users/trishume/following{/other_user}","gists_url":"https://api.github.com/users/trishume/gists{/gist_id}","starred_url":"https://api.github.com/users/trishume/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/trishume/subscriptions","organizations_url":"https://api.github.com/users/trishume/orgs","repos_url":"https://api.github.com/users/trishume/repos","events_url":"https://api.github.com/users/trishume/events{/privacy}","received_events_url":"https://api.github.com/users/trishume/received_events","type":"User","site_admin":false},"created_at":"2016-12-31T16:18:15Z","updated_at":"2016-12-31T16:18:15Z","body":"So that unsafe wrapper can actually do bad things because the lazy loading of regexes modifies the structure, and you don't want that to happen in two threads at the same time.\r\n\r\nSee https://github.com/trishume/syntect/issues/20 for a long overview of the issues and some ways around the issue."}}
{"actor":{"display_login":"trishume"}}