Skip to main content

FixedArray

Struct FixedArray 

Source
pub struct FixedArray<const N: usize, T: Type>(/* private fields */);
Expand description

Declares a fixed-length array type for Specta exporters.

This is primarily useful with #[specta(type = ...)] when you want an array field to keep its length information in exported schemas.

A plain Rust array like [u8; 2] may be exported as number[] if Specta deems it’s unable to safely export it as [number, number]. This limitation is due to the fact that Specta can’t track the inference of const generics and hence can’t fully support them. Using FixedArray<N, T> will always encode the length so it can be used to force override Specta’s conservative behaviour when you know what your doing.

use specta::Type;

/// #[derive(Type)]
struct DemoA {
    a: [u8; 2],     // becomes `[number, number]`

    #[specta(type = specta_util::FixedArray<2, u8>)]
    d: [u8; 2],     // becomes `[number, number]`
}

#[derive(Type)]
struct DemoB<const N: usize = 1> {
    // These are generalised by Specta as we can't know if a specific type is using `N` or a constant, and we don't know what `N` is.
    // If you `#[specta(inline)]` or `#[serde(flatten)]` the `[number, number]` will be restored as we are able to track it properly.
    data: [u32; N], // becomes `number[]`
    a: [u8; 2],     // becomes `number[]`

    #[specta(type = specta_util::FixedArray<2, u8>)]
    d: [u8; 2],     // becomes `[number, number]`
}

Trait Implementations§

Source§

impl<const N: usize, T: Type> Type for FixedArray<N, T>

Source§

fn definition(types: &mut Types) -> DataType

Returns a DataType that represents Self. Read more

Auto Trait Implementations§

§

impl<const N: usize, T> Freeze for FixedArray<N, T>

§

impl<const N: usize, T> RefUnwindSafe for FixedArray<N, T>
where T: RefUnwindSafe,

§

impl<const N: usize, T> Send for FixedArray<N, T>
where T: Send,

§

impl<const N: usize, T> Sync for FixedArray<N, T>
where T: Sync,

§

impl<const N: usize, T> Unpin for FixedArray<N, T>
where T: Unpin,

§

impl<const N: usize, T> UnsafeUnpin for FixedArray<N, T>

§

impl<const N: usize, T> UnwindSafe for FixedArray<N, 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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