Skip to main content

Res

Struct Res 

Source
pub struct Res<T: Serialize = ()> {
    pub code: i32,
    pub msg: String,
    pub data: Option<T>,
}
Expand description

统一 API 响应结构

Fields§

§code: i32

业务码,0 表示成功

§msg: String

提示信息

§data: Option<T>

数据载荷

Implementations§

Source§

impl Res<()>

Source

pub fn ok_empty() -> Self

成功(无数据载荷),返回 {code: 0, msg: "ok", data: null}

Source

pub fn ok_msg(msg: impl Into<String>) -> Self

成功(自定义消息,无数据载荷)

Source§

impl<T: Serialize> Res<T>

Source

pub fn ok(data: T) -> Self

成功响应,携带数据载荷

§示例
Res::ok(user)          // => {code: 0, msg: "ok", data: user}
Res::ok("hello")       // => {code: 0, msg: "ok", data: "hello"}
Source

pub fn ok_with_msg(data: T, msg: impl Into<String>) -> Self

成功响应,携带数据载荷和自定义消息

Source

pub fn fail(code: i32, msg: impl Into<String>) -> Self

失败响应(自定义错误码和消息,无数据载荷)

§示例
Res::fail(codes::BAD_REQUEST, "用户名不能为空")
Source§

impl<T: Serialize> Res<PageData<T>>

Source

pub fn page(list: T, total: u64, page: u64, page_size: u64) -> Self

分页响应

Trait Implementations§

Source§

impl<T: Clone + Serialize> Clone for Res<T>

Source§

fn clone(&self) -> Res<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Serialize> Debug for Res<T>

Source§

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

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

impl<T> Serialize for Res<T>
where T: Serialize + Serialize,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Res<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Res<T>
where T: RefUnwindSafe,

§

impl<T> Send for Res<T>
where T: Send,

§

impl<T> Sync for Res<T>
where T: Sync,

§

impl<T> Unpin for Res<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Res<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Res<T>
where T: UnwindSafe,

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