Skip to content

TokenSink trait forces interior mutability/RefCell usage #642

Open
@LunNova

Description

@LunNova

pub trait TokenSink {
type Handle;
/// Process a token.
fn process_token(&self, token: Token, line_number: u64) -> TokenSinkResult<Self::Handle>;
// Signal sink that tokenization reached the end.
fn end(&self) {}
/// Used in the markup declaration open state. By default, this always
/// returns false and thus all CDATA sections are tokenized as bogus
/// comments.
/// <https://html.spec.whatwg.org/multipage/#markup-declaration-open-state>
fn adjusted_current_node_present_but_not_in_html_namespace(&self) -> bool {
false
}
}

The TokenSink trait's process_token method takes &self

Many usecases for a TokenSink are going to involve mutability. This includes multiple files in this repo such as tree_builder.

I think TokenSink would be better like this:

pub trait TokenSink {
    type Handle;
    fn process_token(self, token: Token, line_number: u64) -> TokenSinkResult<Self::Handle>;
    fn end(self);
    fn adjusted_current_node_present_but_not_in_html_namespace(self);
}

Usecases that need mutability could then be impl TokenSink for &mut MyTokenSink, and those that don't like the printer examples can be impl TokenSink for &MyType.
I haven't tried implementing this refactor, it might not actually work out well.

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