How to implement AsRef for a struct containing references

fabiim

If I have a struct containing a reference like so:

struct Struct<'a> {
    reference: &'a str
}

How can I implement AsRef for the Struct? I tried this:

impl<'a> AsRef<Struct<'a>> for Struct<'a> {
    fn as_ref(&self) -> &Struct {
        self
    }
}

but it fails to satisfy the compiler requirements:

cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements

user2722968

With fn as_ref(&self) -> &Struct the compiler has to infer the (implicit) generic lifetime in the return type and fails to do so. The compiler is expecting a Struct<'a> but the signature promises a free parameter. That's why you get

      expected fn(&Struct<'a>) -> &Struct<'a>
         found fn(&Struct<'a>) -> &Struct<'_>  // '_ is some anonymous lifetime,
                                               // which needs to come from somewhere

The solution is to modify the signature to return Struct<'a> instead of Struct. Even shorter and more clear:

impl<'a> AsRef<Struct<'a>> for Struct<'a> {
    fn as_ref(&self) -> &Self {  // Self is Struct<'a>, the type for which we impl AsRef
        self
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to implement operators for a Rust struct when the operands might be values or references?

Struct containing references to self OR other struct type?

How do I implement Debug for a struct containing a function type alias?

Implement `AsRef` and `Borrow` on `Cow`

julia implement convert for struct containing NTuple

Implement Iterator trait for a struct containing an iterable field

how to iterate a vector containing references?

How to resolve references in Unmarshaled struct

How to create a struct with a vector of references?

How to implement a C# delegate containing an in-struct parameter in VB.NET?

How to implement a list of references in python?

How to use `AsRef` arguments?

How to implement generics struct correctly

Can a struct containing a raw pointer implement Send and be FFI safe?

How to idiomatically construct struct with heap references?

Why does T not implement AsRef<T>?

How does AsRef work with str?

How to create a Quickcheck Arbitrary of a struct containing a reference?

How to pass struct containing matrices in Cuda

How to search an Array containing struct elements in Swift?

How to make a struct field containing an Arc writable?

How to finalize lisp:struct containing a pointer?

How to fill array of struct containing pointer arrays

How to pass struct containing array to the kernel in CUDA?

Lifetimes and references to objects containing references

How to reflect fields of containing struct from a method of the embedded struct?

How to Handle Encoding a Swift Struct Containing Swift Struct as Property

How to implement an enum containing a Cow of a vector of itself?

How to implement Racket-style struct in scheme?