diff --git a/actix-limitation/CHANGES.md b/actix-limitation/CHANGES.md index d914e8f89..f17fd2176 100644 --- a/actix-limitation/CHANGES.md +++ b/actix-limitation/CHANGES.md @@ -1,7 +1,9 @@ # Changes ## Unreleased - 2022-xx-xx -- Updated `session-session` dependency to `0.7`. +- `Limiter::builder` now takes an `impl Into`. +- Removed lifetime from `Builder`. +- Updated `actix-session` dependency to `0.7`. ## 0.2.0 - 2022-03-22 diff --git a/actix-limitation/src/builder.rs b/actix-limitation/src/builder.rs index 99072fe2d..3bc316037 100644 --- a/actix-limitation/src/builder.rs +++ b/actix-limitation/src/builder.rs @@ -4,17 +4,17 @@ use redis::Client; use crate::{errors::Error, Limiter}; -/// Rate limit builder. +/// Rate limiter builder. #[derive(Debug)] -pub struct Builder<'a> { - pub(crate) redis_url: &'a str, +pub struct Builder { + pub(crate) redis_url: String, pub(crate) limit: usize, pub(crate) period: Duration, pub(crate) cookie_name: Cow<'static, str>, pub(crate) session_key: Cow<'static, str>, } -impl Builder<'_> { +impl Builder { /// Set upper limit. pub fn limit(&mut self, limit: usize) -> &mut Self { self.limit = limit; @@ -45,7 +45,7 @@ impl Builder<'_> { /// **synchronous** operation. pub fn build(&self) -> Result { Ok(Limiter { - client: Client::open(self.redis_url)?, + client: Client::open(self.redis_url.as_str())?, limit: self.limit, period: self.period, cookie_name: self.cookie_name.clone(), @@ -63,7 +63,7 @@ mod tests { let redis_url = "redis://127.0.0.1"; let period = Duration::from_secs(10); let builder = Builder { - redis_url, + redis_url: redis_url.to_owned(), limit: 100, period, cookie_name: Cow::Owned("session".to_string()), @@ -82,7 +82,7 @@ mod tests { let redis_url = "redis://127.0.0.1"; let period = Duration::from_secs(20); let mut builder = Builder { - redis_url, + redis_url: redis_url.to_owned(), limit: 100, period: Duration::from_secs(10), session_key: Cow::Borrowed("key"), @@ -109,7 +109,7 @@ mod tests { let redis_url = "127.0.0.1"; let period = Duration::from_secs(20); let mut builder = Builder { - redis_url, + redis_url: redis_url.to_owned(), limit: 100, period: Duration::from_secs(10), session_key: Cow::Borrowed("key"), diff --git a/actix-limitation/src/lib.rs b/actix-limitation/src/lib.rs index 43c02c1d9..35da4e423 100644 --- a/actix-limitation/src/lib.rs +++ b/actix-limitation/src/lib.rs @@ -88,9 +88,9 @@ impl Limiter { /// See [`redis-rs` docs](https://docs.rs/redis/0.21/redis/#connection-parameters) on connection /// parameters for how to set the Redis URL. #[must_use] - pub fn builder(redis_url: &str) -> Builder<'_> { + pub fn builder(redis_url: impl Into) -> Builder { Builder { - redis_url, + redis_url: redis_url.into(), limit: DEFAULT_REQUEST_LIMIT, period: Duration::from_secs(DEFAULT_PERIOD_SECS), cookie_name: Cow::Borrowed(DEFAULT_COOKIE_NAME),