Skip to content

lifetimes #4

Open
Open
@Redhawk18

Description

@Redhawk18

Hi, the last big problem I was having is with the lifetimes with syntax. I wanted to ask why do we need a reference? Why can't the highlighter type take ownership of the variables required?

pub struct Loader {}

impl Loader {
    pub fn new() -> Self {
        Self {}
    }
}

impl tree_house::LanguageLoader for Loader {
    fn language_for_marker(
        &self,
        marker: tree_house::InjectionLanguageMarker,
    ) -> Option<tree_house::Language> {...}

    fn get_config(&self, lang: tree_house::Language) -> Option<&tree_house::LanguageConfig> {...}
}

pub struct Highlighter(highlighter::Highlighter<'static, 'static, Loader>);

#[derive(Clone, PartialEq)]
pub struct Settings {}

impl text::Highlighter for Highlighter { // <--- static trait bound
    type Settings = Settings;
    type Highlight = highlighter::Highlight;
    type Iterator<'a> = Box<dyn Iterator<Item = (Range<usize>, highlighter::Highlight)> + 'a>; // <--- This would have to turn into b

    fn new(settings: &Self::Settings) -> Self {
        let language = Language(0 as u32);
        let timeout = Duration::from_millis(250);
        let loader = Loader::new();
        let syntax = Syntax::new("todo".into(), language, timeout, &loader)
            .expect("Failed to create syntax");

        Self(highlighter::Highlighter::new(
            &syntax, // <--- 'tree required
            "".into(),
            &loader, // <--- 'a required
            0..5,
        ))
    }

As you can see this quickly becomes a nightmare very fast.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions