After playing around with newtypes a bit, I'm curious about this use of the AsRef trait:

impl PaliSearcher { pub fn new(index: &PaliIndex) -> Result<Self> { let reader = Self::reader(index.as_ref())?; let uid = index.uid_field()?; let contents = index.contents_field()?; let query_parser = QueryParser::for_index(index.as_ref(), vec![contents]); Ok(Self { reader, query_parser, uid, contents, }) } fn reader(index: &Index) -> Result<IndexReader>{ let reader = index .reader_builder() .reload_policy(ReloadPolicy::OnCommitWithDelay) .try_into()?; Ok(reader) } ... 

I have this AsRef implementation:

impl AsRef<Index> for PaliIndex { fn as_ref(&self) -> &Index { &self.index } } 

But when I change the as_ref() to a normal reference like so:

let reader = Self::reader(&index)?; 

I get this error:

error[E0308]: mismatched types --> src/search.rs:18:35 | 18 | let reader = Self::reader(&index)?; | ------------ ^^^^^^ expected `&Index`, found `&&PaliIndex` | | | arguments to this function are incorrect | = note: expected reference `&tantivy::Index` found reference `&&PaliIndex` note: associated function defined here --> src/search.rs:31:8 | 31 | fn reader(index: &Index) -> Result<IndexReader>{ | ^^^^^^ ------------- 

1 post - 1 participant

Read full topic