[Solved]Questions about how to have multiple clients for different namespaces of an HTTP API client sharing an access_token and some lifetime issues

I want to write a client to an api and want to seperate the different namespaces in different clients which would have the operations for each namespace.

They all share the same access code which the user could pass in to the constructor of the overall api client, the client then passes it to the inner clients for the specific namespaces. I thought of using a Cow<'a, str> for the inner clients since the access_code should be the same for all client and shouldn't be modified by the InnerClient. I am having trouble working out the lifetimes when initializing the client with a String.

  • Simplest with String This one works and doesn't need lifetimes but duplicates the string where it's not really necessary.

  • With Cow<'a, str> I believe would share the same underlying string but having trouble working out the lifetimes.

Am I going about this the right way or is there a better way of doing this?

Sounds more like a usecase for Rc<String>.

Cow<'a, str> is not for shared ownership, it's to allow working with a (borrowed) string slice or an owned string. But if it's using a borrowed slice, it'll have a lifetime dependent on the lifetime of the slice itself (hence the lifetime parameter in Cow).

Thanks you are right. I was misusing the Cow type.

Fixed it here and it works