Skip to main content

Config

Struct Config 

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

§零拷贝配置共享实现

Config 内部使用 Arc<ConfigInner> 实现零拷贝共享:

§性能特性

  • 内存效率: 所有克隆共享同一份配置数据(~300-500字节)
  • 克隆成本: Config::clone() 只复制Arc指针(8字节 + 原子操作)
  • 线程安全: Arc保证多线程安全的只读访问
  • 引用计数: 自动管理内存,无泄漏风险

§使用建议

// ✅ 推荐: 克隆Config传递给服务
let service = MyService::new(config.clone());

// ✅ 推荐: 在Request中持有Config
pub struct MyRequest {
    config: Config,  // 持有Arc指针,成本低
}

// ⚠️ 不必要: 使用Arc<Config> (Config内部已经是Arc)
// Arc<Arc<ConfigInner>> = 双重Arc,没有额外收益

§性能验证

运行 cargo test config_arc 查看基准测试:

  • 克隆速度: ~10-20纳秒
  • 内存开销: 每个克隆仅8字节
  • 引用计数: 自动维护

Implementations§

Source§

impl Config

Source

pub fn builder() -> ConfigBuilder

Source

pub fn new(inner: ConfigInner) -> Config

创建新的 Config 实例,直接从 ConfigInner

Source

pub fn with_token_provider( &self, provider: impl TokenProvider + 'static, ) -> Config

基于当前配置生成一个“替换 TokenProvider”的新配置

说明:

  • 这是一个纯拷贝操作(Config 本身是 Arc 包装),不会修改原配置
  • 推荐用法:先构建一个“基础 Config”(默认 NoOpTokenProvider),再用该基础 Config 构建业务 TokenProvider, 最后调用此方法把 provider 注入到“业务 Config”中,避免循环引用。
Source

pub fn reference_count(&self) -> usize

获取内部 Arc 的引用计数

Source

pub fn app_id(&self) -> &str

获取应用 ID

Source

pub fn app_secret(&self) -> &str

获取应用密钥

Source

pub fn base_url(&self) -> &str

获取基础 URL

Source

pub fn req_timeout(&self) -> Option<Duration>

获取超时时间

Source

pub fn enable_token_cache(&self) -> bool

是否启用令牌缓存

Source

pub fn app_type(&self) -> AppType

获取应用类型

Source

pub fn http_client(&self) -> &Client

获取 HTTP 客户端引用

Source

pub fn header(&self) -> &HashMap<String, String>

获取自定义 header 引用

Source

pub fn token_provider(&self) -> &Arc<dyn TokenProvider>

获取 TokenProvider 引用

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

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
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl Deref for Config

Source§

type Target = ConfigInner

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Config as Deref>::Target

Dereferences the value.

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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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<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