RTokenManager

Struct RTokenManager 

Source
pub struct RTokenManager { /* private fields */ }
Expand description

Token Manager | Token 管理器

RTokenManager is the core component of r-token library, responsible for managing user token lifecycle.

RTokenManager 是 r-token 库的核心组件,负责管理用户的 Token 生命周期。

§Features | 特点

  • Thread-safe | 线程安全: Safe multi-threaded access with Arc<Mutex<HashMap>> | 使用 Arc<Mutex<HashMap>> 实现多线程环境下的安全访问
  • Cloneable | 可克隆: Implements Clone trait for sharing across actix-web workers | 实现了 Clone trait,可以在多个 actix-web worker 之间共享
  • Simple | 简单易用: Provides two core methods: login and logout | 提供 loginlogout 两个核心方法

§Example | 示例

use r_token::RTokenManager;

let manager = RTokenManager::new();
let token = manager.login("user123");
println!("Generated token: {}", token);

// Later... | 稍后...
manager.logout(&token);

Implementations§

Source§

impl RTokenManager

Source

pub fn new() -> Self

Create a new Token Manager instance | 创建一个新的 Token 管理器实例

This method initializes an empty token storage. In an actix-web application, it’s typically called once in the main function, then injected into the app via app_data.

这个方法会初始化一个空的 Token 存储。在 actix-web 应用中, 通常在 main 函数中调用一次,然后通过 app_data 注入到应用中。

§Example | 示例
use r_token::RTokenManager;
use actix_web::{web, App};

let manager = RTokenManager::new();
// Usage in actix-web | 在 actix-web 中使用
// App::new().app_data(web::Data::new(manager.clone()))
Source

pub fn login(&self, id: &str) -> String

User login: Generate and store Token | 用户登录:生成 Token 并存储

This method will: | 此方法会:

  1. Generate a new UUID v4 as Token | 生成一个新的 UUID v4 作为 Token
  2. Store the mapping between Token and User ID in memory | 将 Token 和用户 ID 的映射关系存入内存
  3. Return the generated Token string | 返回生成的 Token 字符串
§Parameters | 参数
  • id: User’s unique identifier (usually user ID) | 用户的唯一标识符(通常是用户 ID)
§Returns | 返回值

Returns a newly generated Token string (UUID v4 format) | 返回一个新生成的 Token 字符串(UUID v4 格式)

§Example | 示例
use r_token::RTokenManager;

let manager = RTokenManager::new();
let token = manager.login("user123");
assert!(!token.is_empty());
Source

pub fn logout(&self, token: &str)

User logout: Remove Token | 用户登出:移除 Token

This method removes the specified Token from memory, invalidating it. Invalidated tokens will fail validation through the RUser extractor.

此方法会从内存中删除指定的 Token,使其失效。 失效后的 Token 将无法通过 RUser extractor 的验证。

§Parameters | 参数
  • token: The Token string to invalidate | 要注销的 Token 字符串
§Example | 示例
use r_token::RTokenManager;

let manager = RTokenManager::new();
let token = manager.login("user123");

// User logout | 用户登出
manager.logout(&token);
// Token is now invalid | 此时 token 已失效

Trait Implementations§

Source§

impl Clone for RTokenManager

Source§

fn clone(&self) -> RTokenManager

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more