Skip to content

spa: ID Macros

Matthieu Legrand requested to merge oxabz/pipewire-rs:id-enums into main

The merge request has been totally change this is not true anymore

trait IdEnum :

A trait that represent IDs as an Enum

IdEnums can :

  • be cast to and from u32
  • be cast to and from Id
  • be serialized and deserialize as a CanonicalFixedSizedPod

attribute_proc_macro id_enum:

An attribute macro that takes a regex and an enum and adds the variants to the enums according to the const in spa_sys.

Example :

#[id_enum(r"SPA_MEDIA_TYPE_([a-z0-9]+)")]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, IdEnum)]
enum MediaType {
    // Generated by spa-macros
}

expands to

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, IdEnum)]
enum MediaType {
    #[doc = "val : 1"]
    Audio = spa_sys::SPA_MEDIA_TYPE_audio,
    #[doc = "val : 6"]
    Application = spa_sys::SPA_MEDIA_TYPE_application,
    #[doc = "val : 3"]
    Image = spa_sys::SPA_MEDIA_TYPE_image,
    #[doc = "val : 4"]
    Binary = spa_sys::SPA_MEDIA_TYPE_binary,
    #[doc = "val : 5"]
    Stream = spa_sys::SPA_MEDIA_TYPE_stream,
    #[doc = "val : 0"]
    Unknown = spa_sys::SPA_MEDIA_TYPE_unknown,
    #[doc = "val : 2"]
    Video = spa_sys::SPA_MEDIA_TYPE_video,
}

Design notes :

  • The way the bindings are obtained are a bit janky (see libspa-macro/constants.rs) I'm open for subjections to pass a path from libspa-sys to libspa-macro
  • The macro doesn't require a #[repr(u32)] even though it's required. (The compiler raise an error on its own)

derive IdEnum (aka : id_enum_derive)

A derive macro for IdEnum

Design notes :

  • the implementation of to_u32 expect #[repr(u32)]
  • the implementation of to_u32 relies on transmute_copy it should work but could cause problem with other reprs than #[repr(u32)]
Edited by Matthieu Legrand

Merge request reports

Loading