How to name lifetimes for Self?

โš“ Rust    ๐Ÿ“… 2025-08-07    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 5      

surdeus

First and foremost, Iโ€™m genuinely opposed to naming lifetimes like โ€™a and โ€™b, since those names are non-descriptive.

I am currently implementing an API to interact with Mender servers. Within the crate I have traits for the several endpoints like this:

/// Deployments management API.
pub trait Deployments<'this> {
    /// List deployments.
    fn list(
        self,
        page_size: Option<NonZero<usize>>,
    ) -> PageIterator<'this, 'static, ListDeployment>;

    /// Create a new deployment.
    fn create(
        self,
        deployment: &NewDeployment,
    ) -> impl Future<Output = reqwest::Result<String>> + Send;

    /// Collect deployments into a `Vec`.
    fn collect(
        self,
        page_size: Option<NonZero<usize>>,
    ) -> impl Future<Output = reqwest::Result<Vec<ListDeployment>>> + Send;
}

impl<'session> Deployments<'session> for &'session Session {
    fn list(
        self,
        page_size: Option<NonZero<usize>>,
    ) -> PageIterator<'session, 'static, ListDeployment> {
        Pager::new(self, PATH, page_size.unwrap_or(DEFAULT_PAGE_SIZE)).into()
    }

    async fn create(self, deployment: &NewDeployment) -> reqwest::Result<String> {
        self.client()
            .post(self.format_url(PATH, None))
            .bearer_auth(self.bearer_token())
            .json(deployment)
            .send()
            .await?
            .error_for_status()?
            .text()
            .await
    }

    async fn collect(
        self,
        page_size: Option<NonZero<usize>>,
    ) -> reqwest::Result<Vec<ListDeployment>> {
        Pager::new(self, PATH, page_size.unwrap_or(DEFAULT_PAGE_SIZE))
            .collect()
            .await
    }
}

I really struggled with naming the lifetime for pub trait Deployments<'this>, since my first attempt was to use โ€™self, which the language unfortunately forbids.

Is there a common guideline to naming lifetimes that represent the lifetime of the traitโ€™s implementor?

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed