windows-bindgen 0.101.0

Code generator for Windows metadata
Documentation
use super::*;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Param {
    pub def: Option<MethodParam>,
    pub name: String,
    pub ty: Type,
    /// Unspecified native direction whose type passes no caller-writable storage, or a legacy
    /// Win32 parameter without `Out`.
    pub implied_input: bool,
    /// Legacy only: the enum accepted by an input integer parameter with `AssociatedEnumAttribute`.
    pub associated_enum: Option<Type>,
}

impl std::ops::Deref for Param {
    type Target = Type;

    fn deref(&self) -> &Self::Target {
        &self.ty
    }
}

impl Param {
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn attributes(
        &self,
    ) -> impl Iterator<Item = windows_metadata::reader::Attribute<'static>> + '_ {
        self.def.into_iter().flat_map(|def| def.attributes())
    }

    pub fn has_attribute(&self, name: &str) -> bool {
        self.def.is_some_and(|def| def.has_attribute(name))
    }

    pub fn is_convertible(&self) -> bool {
        self.is_input_only() && self.ty.is_convertible()
    }

    pub fn is_convertible_with_reader(&self, reader: &Reader) -> bool {
        self.is_input_only() && self.projection_type(reader).is_convertible()
    }

    pub fn projection_type(&self, reader: &Reader) -> Type {
        self.ty.projection_type(reader)
    }

    pub fn projection_pointee(&self, reader: &Reader) -> Type {
        self.ty.projection_pointee(reader)
    }

    /// Returns the inner type for input `IReference<T>` parameters exposed as `Option<T>`.
    pub fn ireference_inner(&self, reader: &Reader) -> Option<&Type> {
        if !self.is_input_only() {
            return None;
        }
        self.ty.ireference_inner_for_sugar(reader)
    }

    /// Returns whether metadata marks this parameter as input-only, or its unspecified direction
    /// is implied input by its native type.
    pub fn is_input_only(&self) -> bool {
        self.implied_input
            || self.def.is_some_and(|def| {
                def.direction() == windows_metadata::reader::ParamDirection::Input
            })
    }

    pub fn has_direction(&self) -> bool {
        self.implied_input
            || self.def.is_some_and(|def| {
                def.direction() != windows_metadata::reader::ParamDirection::Unspecified
            })
    }

    /// Returns whether metadata marks this parameter as `Output` without `Input`.
    pub fn is_metadata_output_only(&self) -> bool {
        self.def
            .is_some_and(|def| def.direction() == windows_metadata::reader::ParamDirection::Output)
    }

    /// Returns whether Rust's projection permits omitted storage for an optional or reserved
    /// parameter.
    pub fn is_optional_or_reserved(&self) -> bool {
        self.def
            .is_some_and(|def| def.is_optional() || def.is_reserved())
    }

    pub fn is_retval_attribute(&self) -> bool {
        self.def.is_some_and(|def| def.is_retval_attribute())
    }

    pub fn has_buffer_attributes(&self) -> bool {
        self.buffer_attributes().next().is_some()
    }

    fn buffer_attributes(
        &self,
    ) -> impl Iterator<Item = windows_metadata::reader::Attribute<'static>> + '_ {
        self.attributes().filter(|attribute| {
            attribute.namespace() == "Windows.Win32.Metadata"
                && attribute.name() == "BufferAttribute"
        })
    }

    pub fn has_unresolved_contracts(&self) -> bool {
        self.attributes().any(|attribute| {
            attribute.namespace() == "Windows.Win32.Metadata"
                && attribute.name() == "IncompleteAttribute"
        })
    }

    /// Only an explicit `[retval]` annotation projects the trailing parameter as the result.
    pub fn is_retval_candidate(&self, reader: &Reader) -> bool {
        if !self.is_retval_attribute() || !self.projection_type(reader).is_pointer() {
            return false;
        }

        let Some(def) = self.def else {
            return false;
        };

        if def.direction() != windows_metadata::reader::ParamDirection::Output
            || def.is_optional()
            || def.is_reserved()
        {
            return false;
        }

        !self.has_buffer_attributes() && !self.has_unresolved_contracts()
    }

    pub fn write_ident(&self) -> TokenStream {
        to_ident(&self.name.to_lowercase())
    }
}