pub struct TenantGuard { /* private fields */ }Expand description
在 await 前捕获的租户 ID 持有者
§设计目的
TenantContext 基于 thread_local,在 tokio .await 后可能切换到不同线程,
导致 TenantContext::current() 返回不同值(或 None)。
TenantGuard 在 await 前捕获 tenant_id,将其作为普通字段持有,
之后通过 guard.tenant_id() 访问,不受线程切换影响。
§使用规范
- 在中间件设置
TenantContext::set_current()后,第一个 await 前调用TenantContext::guard()?创建 guard - 跨 await 的业务逻辑使用
guard.tenant_id()而非TenantContext::current() - 如需验证当前线程上下文仍有效,调用
guard.assert_current()
§限制
- Guard 本身是
Copy,可自由跨 await 传递 - Guard 不自动验证 thread_local 一致性 — 需显式调用
assert_current()
Implementations§
Source§impl TenantGuard
impl TenantGuard
Sourcepub fn assert_current(&self) -> Result<(), TenantError>
pub fn assert_current(&self) -> Result<(), TenantError>
验证当前 thread_local 上下文与捕获值一致
返回 Err(TenantError::TenantMismatch) 如果:
- 当前线程的
TenantContext未设置 - 当前线程的
TenantContext与捕获值不同
§使用场景
- 在关键操作前验证上下文完整性
- 调试时检测中间件是否正确设置了租户上下文
Trait Implementations§
Source§impl Clone for TenantGuard
impl Clone for TenantGuard
Source§fn clone(&self) -> TenantGuard
fn clone(&self) -> TenantGuard
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl Copy for TenantGuard
Source§impl Debug for TenantGuard
impl Debug for TenantGuard
impl Eq for TenantGuard
Source§impl PartialEq for TenantGuard
impl PartialEq for TenantGuard
impl StructuralPartialEq for TenantGuard
Auto Trait Implementations§
impl Freeze for TenantGuard
impl RefUnwindSafe for TenantGuard
impl Send for TenantGuard
impl Sync for TenantGuard
impl Unpin for TenantGuard
impl UnsafeUnpin for TenantGuard
impl UnwindSafe for TenantGuard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
impl<T> Scalar for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.