Skip to main content

ShapeBuilder

Struct ShapeBuilder 

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

Builder for creating Shape instances.

This builder provides a convenient way to construct Shape values with sensible defaults. Many fields can be inferred or have reasonable defaults:

ShapeBuilder::for_sized::<MyType>("MyType")
    .def(Def::Scalar)
    .vtable(my_vtable)
    .build()

Implementations§

Source§

impl ShapeBuilder

Source

pub const fn for_sized<T>(type_identifier: &'static str) -> Self

Create a new builder for a sized type.

The id and layout are derived from the type parameter.

Source

pub const fn for_unsized<T: ?Sized>(type_identifier: &'static str) -> Self

Create a new builder for an unsized type.

Source

pub const fn vtable(self, vtable: VTableErased) -> Self

Set the vtable (type-erased).

Source

pub const fn vtable_direct(self, vtable: &'static VTableDirect) -> Self

Set the vtable from a direct vtable reference.

Source

pub const fn vtable_indirect(self, vtable: &'static VTableIndirect) -> Self

Set the vtable from an indirect vtable reference.

Source

pub const fn type_ops(self, type_ops: TypeOps) -> Self

Set the per-type operations (drop, default, clone) using the erased enum.

For generic containers, use this to provide the monomorphized operations while sharing the main vtable across all instantiations.

Source

pub const fn type_ops_direct(self, type_ops: &'static TypeOpsDirect) -> Self

Set per-type operations for concrete types (uses thin pointers).

Use this for scalars, String, and derived structs/enums.

Source

pub const fn type_ops_indirect(self, type_ops: &'static TypeOpsIndirect) -> Self

Set per-type operations for generic containers (uses wide pointers).

Use this for Vec<T>, Option<T>, Arc<T>, etc.

Source

pub const fn add_marker_trait(self, trait_flag: MarkerTraits) -> Self

Add a marker trait flag.

Source

pub const fn marker_traits(self, traits: MarkerTraits) -> Self

Set all marker traits at once using combined bitflags.

Source

pub const fn eq(self) -> Self

Mark type as implementing Eq.

Source

pub const fn copy(self) -> Self

Mark type as implementing Copy.

Source

pub const fn send(self) -> Self

Mark type as implementing Send.

Source

pub const fn sync(self) -> Self

Mark type as implementing Sync.

Source

pub const fn unpin(self) -> Self

Mark type as implementing Unpin.

Source

pub const fn unwind_safe(self) -> Self

Mark type as implementing UnwindSafe.

Source

pub const fn ref_unwind_safe(self) -> Self

Mark type as implementing RefUnwindSafe.

Source

pub const fn ty(self, ty: Type) -> Self

Set the type.

Source

pub const fn def(self, def: Def) -> Self

Set the definition.

Source

pub const fn module_path(self, module_path: &'static str) -> Self

Set the module path where this type is defined.

This is typically set to module_path!() by the derive macro, which expands to the module path at the definition site.

Source

pub const fn decl_id(self, decl_id: DeclId) -> Self

Set the declaration identifier.

The DeclId identifies the type declaration independent of type parameters. For derive macros, this is typically computed from the source location and type name.

For non-generic types (no type parameters), you don’t need to call this - the decl_id will be automatically computed from the type identifier.

For generic foreign types, just set module_path and the decl_id will be auto-computed from @module_path#kind#type_identifier.

See DeclId for stability guarantees (spoiler: there are none).

Source

pub const fn source_file(self, file: &'static str) -> Self

Set the source file where this type is defined.

This is typically set to file!() by the derive macro when the doc feature is enabled.

Source

pub const fn source_line(self, line: u32) -> Self

Set the source line number where this type is defined.

This is typically set to line!() by the derive macro when the doc feature is enabled.

Source

pub const fn source_column(self, column: u32) -> Self

Set the source column number where this type is defined.

This is typically set to column!() by the derive macro when the doc feature is enabled.

Source

pub const fn type_params(self, type_params: &'static [TypeParam]) -> Self

Set the type parameters.

Source

pub const fn doc(self, doc: &'static [&'static str]) -> Self

Set the documentation.

Source

pub const fn attributes(self, attributes: &'static [Attr]) -> Self

Set the attributes.

Source

pub const fn type_tag(self, type_tag: &'static str) -> Self

Set the type tag.

Source

pub const fn inner(self, inner: &'static Shape) -> Self

Set the inner shape (for transparent/newtype wrappers).

Source

pub const fn builder_shape(self, builder: &'static Shape) -> Self

Set the builder shape for immutable collections.

If set, deserializers will build the value using the builder shape, then convert to the target type. Used for immutable collections like Bytes (builds through BytesMut) or Arc<[T]> (builds through Vec<T>).

Source

pub const fn type_name(self, type_name: TypeNameFn) -> Self

Set the type name function for formatting generic type names.

For generic types like Vec<T>, this function formats the full name including type parameters (e.g., Vec<String>).

Source

pub const fn proxy(self, proxy: &'static ProxyDef) -> Self

Set the container-level proxy for custom serialization/deserialization.

When a proxy is set, the type will be serialized/deserialized through the proxy type instead of directly.

Source

pub const fn format_proxies(self, proxies: &'static [FormatProxy]) -> Self

Set the format-specific container-level proxy definitions.

Format-specific proxies take precedence over the format-agnostic proxy when the format matches. Use this for types that need different representations in different formats (e.g., XML vs JSON).

Source

pub const fn variance(self, variance: VarianceDesc) -> Self

Set the variance description for this type.

For types that propagate inner variance, use VarianceDesc::propagate(inner_shape). For types with constant variance, use VarianceDesc::BIVARIANT or VarianceDesc::INVARIANT. For complex types like function pointers, construct with VarianceDesc::new(base, deps).

Source

pub const fn flags(self, flags: ShapeFlags) -> Self

Set the flags for this shape.

Source

pub const fn untagged(self) -> Self

Mark this enum as untagged.

Untagged enums serialize their content directly without any discriminant.

Source

pub const fn tag(self, tag: &'static str) -> Self

Set the tag field name for internally/adjacently tagged enums.

Source

pub const fn content(self, content: &'static str) -> Self

Set the content field name for adjacently tagged enums.

Source

pub const fn rename(self, rename: &'static str) -> Self

Set the rename for this shape.

Source

pub const fn is_numeric(self) -> Self

Mark this enum as numeric.

Numeric enums serialize to the underlying discriminant

Source

pub const fn pod(self) -> Self

Mark this type as Plain Old Data.

POD types have no invariants - any combination of valid field values produces a valid instance. This enables safe mutation through reflection.

Source

pub const fn metadata_container(self) -> Self

Mark this struct as a metadata container.

Metadata containers serialize transparently through their non-metadata field while preserving metadata fields for formats that support them.

Source

pub const fn build(self) -> Shape

Build the Shape.

If ty was not explicitly set (still Type::Undefined), it will be inferred from def.

§DeclId auto-computation
  • Non-generic types: computed from type_identifier
  • Generic types with module_path: computed from @module_path#kind#type_identifier
  • Generic types without module_path: panics (derive macro sets decl_id explicitly)

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