Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: How to name lifetimes for Self?
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
๐ท๏ธ Rust_feed