{{ self::swift_doc_block(doc, "") }}public struct {{ class_name }}: Hashable, Equatable, Sendable{% if is_error %}, Error{% endif %} {
{%- for field in fields %}
{{ self::swift_doc_block(field.doc, "    ") }}    public var {{ field.swift_name }}: {{ field.swift_type }}
{%- endfor %}

    public init({% for field in fields %}{{ field.swift_name }}: {{ field.swift_type }}{% if let Some(default_expr) = field.default_expr %} = {{ default_expr }}{% endif %}{% if !loop.last %}, {% endif %}{% endfor %}) {
{%- for field in fields %}
        self.{{ field.swift_name }} = {{ field.swift_name }}
{%- endfor %}
    }

}

extension {{ class_name }}: WireCodable {
{%- if is_blittable %}
    @inlinable static func decode(from reader: inout WireReader) -> {{ class_name }} { reader.readBlittable() }
    @inlinable func encode(to writer: inout WireWriter) { writer.writeBlittable(self) }
{%- else %}
    @inlinable static func decode(from reader: inout WireReader) -> {{ class_name }} {
        {{ class_name }}({% for field in fields %}{{ field.swift_name }}: {{ field.wire_reader_decode() }}{% if !loop.last %}, {% endif %}{% endfor %})
    }
    @inlinable func encode(to writer: inout WireWriter) {
{%- for field in fields %}
        {{ field.wire_writer_encode() }}
{%- endfor %}
    }
{%- endif %}
}
{%- if !constructors.is_empty() || !methods.is_empty() %}

extension {{ class_name }} {
{%- for ctor in constructors %}
{%- if ctor.is_factory() %}

{{ self::swift_doc_block(ctor.doc(), "    ") }}    public static func {{ ctor.name().unwrap() }}(){% if ctor.is_fallible() %} throws{% endif %} -> {{ class_name }}{% if ctor.is_optional() %}?{% endif %} {
{%- if ctor.is_optional() %}
        let buf = {{ ctor.ffi_symbol() }}()
        defer { {{ prefix }}_free_buf(buf) }
        return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in reader.readOptional { reader in {{ class_name }}.decode(from: &reader) } }
{%- elif is_blittable %}
{%- if ctor.is_fallible() %}
        let buf = {{ ctor.ffi_symbol() }}()
        defer { {{ prefix }}_free_buf(buf) }
        return try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        let _raw = {{ ctor.ffi_symbol() }}()
        return {{ class_name }}({% for field in fields %}{{ field.swift_name }}: _raw.{{ field.swift_name }}{% if !loop.last %}, {% endif %}{% endfor %})
{%- endif %}
{%- else %}
        let buf = {{ ctor.ffi_symbol() }}()
        defer { {{ prefix }}_free_buf(buf) }
{%- if ctor.is_fallible() %}
        return try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            {{ class_name }}.decode(from: &reader)
        }
{%- endif %}
{%- endif %}
    }
{%- elif ctor.is_designated() %}

{{ self::swift_doc_block(ctor.doc(), "    ") }}    public static func new({% for p in ctor.params() %}{{ p.signature() }}{% if !loop.last %}, {% endif %}{% endfor %}){% if ctor.is_fallible() %} throws{% endif %} -> {{ class_name }}{% if ctor.is_optional() %}?{% endif %} {
{%- for param in ctor.params() %}
{%- if let Some(wrapper) = param.wrapper_code() %}
        {{ wrapper }}
{%- endif %}
{%- endfor %}
{%- if ctor.is_optional() %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
        return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in reader.readOptional { reader in {{ class_name }}.decode(from: &reader) } }
{%- elif is_blittable %}
{%- if ctor.is_fallible() %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
        return try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        let _raw = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        return {{ class_name }}({% for field in fields %}{{ field.swift_name }}: _raw.{{ field.swift_name }}{% if !loop.last %}, {% endif %}{% endfor %})
{%- endif %}
{%- else %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
{%- if ctor.is_fallible() %}
        return try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            {{ class_name }}.decode(from: &reader)
        }
{%- endif %}
{%- endif %}
    }
{%- else %}

{{ self::swift_doc_block(ctor.doc(), "    ") }}    public init{% if ctor.is_optional() %}?{% endif %}({% for p in ctor.params() %}{{ p.signature() }}{% if !loop.last %}, {% endif %}{% endfor %}){% if ctor.is_fallible() %} throws{% endif %} {
{%- for param in ctor.params() %}
{%- if let Some(wrapper) = param.wrapper_code() %}
        {{ wrapper }}
{%- endif %}
{%- endfor %}
{%- if ctor.is_optional() %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
        guard let value: {{ class_name }} = boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len), { reader in reader.readOptional { reader in {{ class_name }}.decode(from: &reader) } }) else { return nil }
        self = value
{%- elif is_blittable %}
{%- if ctor.is_fallible() %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
        self = try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        let _raw = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        self = {{ class_name }}({% for field in fields %}{{ field.swift_name }}: _raw.{{ field.swift_name }}{% if !loop.last %}, {% endif %}{% endfor %})
{%- endif %}
{%- else %}
        let buf = {% for w in ctor.annotated_closure_wrappers() %}{{ w }} {% endfor %}{{ ctor.call_expr() }}{% for _ in ctor.closure_wrappers() %} }{% endfor %}

        defer { {{ prefix }}_free_buf(buf) }
{%- if ctor.is_fallible() %}
        self = try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            try reader.readResultOrThrow(ok: { reader in {{ class_name }}.decode(from: &reader) }, err: { reader in {{ ctor.throw_decode_expr().unwrap() }} })
        }
{%- else %}
        self = boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in
            {{ class_name }}.decode(from: &reader)
        }
{%- endif %}
{%- endif %}
    }
{%- endif %}
{%- endfor %}
{%- for method in methods %}

{{ self::swift_doc_block(method.doc, "    ") }}    public{% if method.is_mutating() %} mutating{% elif method.is_static %} static{% endif %} func {{ method.name }}({% for p in method.params %}{{ p.signature() }}{% if !loop.last %}, {% endif %}{% endfor %}){% if method.returns.is_throws() %} throws{% endif %}{% if !method.mutating_void %}{% if let Some(ret) = method.returns.swift_type() %} -> {{ ret }}{% endif %}{% endif %} {
{%- match method.mode %}
{%- when SwiftCallMode::Sync { symbol } %}
{%- for param in method.params %}
{%- if let Some(wrapper) = param.wrapper_code() %}
        {{ wrapper }}
{%- endif %}
{%- endfor %}
{%- if let Some(rs) = method.value_self %}
{%- if let Some(wrapper) = rs.wrapper_code %}
        {{ wrapper }}
{%- endif %}
{%- endif %}
{%- for open in method.sync_closure_opens() %}
{{ open }}
{%- endfor %}
{%- if method.returns.is_void() %}
{{ method.method_body_indent() }}{{ method.sync_call_expr() }}
{%- elif method.returns.is_wire_encoded() %}
{{ method.method_body_indent() }}let buf = {{ method.sync_call_expr() }}
{{ method.method_body_indent() }}defer { {{ prefix }}_free_buf(buf) }
{%- if let Some(reader_expr) = method.returns.reader_decode_expr() %}
{%- if method.is_mutating() %}
{{ method.method_body_indent() }}self = boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in {{ reader_expr }} }
{%- elif method.returns.is_throws() %}
{{ method.method_body_indent() }}return try boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in {{ reader_expr }} }
{%- else %}
{{ method.method_body_indent() }}return boltffiDecodeOwnedBuf(buf.ptr, Int(buf.len)) { reader in {{ reader_expr }} }
{%- endif %}
{%- endif %}
{%- elif method.returns.is_composite() %}
{%- if let Some(convert_expr) = method.returns.composite_convert_expr("_raw") %}
{{ method.method_body_indent() }}let _raw = {{ method.sync_call_expr() }}
{%- if method.is_mutating() %}
{{ method.method_body_indent() }}self = {{ convert_expr }}
{%- else %}
{{ method.method_body_indent() }}return {{ convert_expr }}
{%- endif %}
{%- endif %}
{%- elif method.returns.is_c_style_enum() %}
{%- if method.is_mutating() %}
{{ method.method_body_indent() }}self = {{ method.returns.c_style_enum_type().unwrap() }}(rawValue: {{ method.sync_call_expr() }})!
{%- else %}
{{ method.method_body_indent() }}return {{ method.returns.c_style_enum_type().unwrap() }}(rawValue: {{ method.sync_call_expr() }})!
{%- endif %}
{%- else %}
{{ method.method_body_indent() }}return {{ method.sync_call_expr() }}
{%- endif %}
{%- for close in method.sync_closure_closes() %}
{{ close }}
{%- endfor %}
{%- when SwiftCallMode::Async { start, poll, complete, cancel, free, result } %}
{%- endmatch %}
    }
{%- endfor %}
}
{%- endif %}
