Generic enum variant as function parameter with trait bounds

So I have a trait Find

trait Find { 
    find(&self, haystack: &[u8]) -> Result<usize, ()>
}

and an enum

enum ReadUntil<T: Find> {
    Parser(T),
    LastNewLine,
}

and a function read_until

fn read_until<T: Find>(&mut self, needle: &ReadUntil<T>, ...) -> Result<> {
    ...
    ...
    ...
    let k = match needle {
                        ReadUntil::Parser(ref parser) => parser.find(search_buf.unwrap_or(&[])),
                        ReadUntil::LastNewLine => search_buf.ok_or(()).map(|b| b.len()),
                    };
     ...
     ...
     ...
}

However, It appears that I am unable to pass ReadUntil::LastNewLine

I get this error,

cannot infer type for type parameter `T` declared on the enum `ReadUntil`

I am unsure of how to proceed. How are able to use None with Option<T> but I can't use LastNewLine. I assume it has to do with the trait bounds on the function? Is that correct? I'd like to enforce that trait bound but also be able to pass in ReadUntil::LastNewLine.

Option::None isn't special. Rust is statically typed (all types must be known at compile time) and sometimes it can't infer the complete type.

You can proceed by supplying the type parameter like the hint says. If your code isn't complete yet, it's also possible that the compiler will be able to infer the type, given enough extra context.

1 Like

ah yes, thank you!