Skip to content

OwnedView has no reborrow accessor for sub-lifetime views #82

@iainmcgin

Description

@iainmcgin

OwnedView<V<'static>> owns the backing Bytes and derefs to V<'static>. There's currently no way to get a V<'_> that borrows from &self with a shorter lifetime - useful when you want to hand a sub-view of the request into something that outlives the deref guard but not the OwnedView itself.

The motivating case is an RPC handler that returns a response borrowing from the request buffer. Today the only zero-copy option is to return the whole OwnedView (refcount bump on the Bytes), which works when the response type == request type but not when the response is a field of the request:

// works: input == output, return the OwnedView wholesale
async fn echo(&self, _: Ctx, req: OwnedView<RecordView<'static>>) -> ServiceResult<...> {
    Response::ok(req)   // refcount bump, zero copy
}

// doesn't work without to_owned(): response is a sub-view
async fn header_of(&self, _: Ctx, req: OwnedView<EnvelopeView<'static>>) -> ServiceResult<...> {
    // want: a HeaderView<'_> borrowing from req's Bytes that the caller
    // can encode before req drops. No accessor for that today.
}

Proposed addition (additive, no signature changes elsewhere):

impl<V: View<'static>> OwnedView<V> {
    /// Reborrow the view with a lifetime tied to `&self`. The returned
    /// view points at the same backing `Bytes`; it's valid as long as
    /// `self` is.
    pub fn reborrow(&self) -> V::Reborrowed<'_> { ... }
}

(or whatever shape fits the existing View/HasOwned machinery - the key property is &'a OwnedView<V<'static>>V<'a>).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions