How to encode structs into bytes?

How do I encode a struct imported from Sway into bytes using Rust SDK?

For example I have a struct defined in Sway


struct TransferParams {
    to: Identity,
    asset_id: ContractId,
    amount: u64,
}

I need to sha256 this struct using Rust so I need the struct encoded as bytes.

Any help? Thanks

I found out that for a simple struct fuels::core::calldata! macro will encode it into bytes.

Care must be taken if the struct contains Bytes and you need to hash it. The hash might be inconsistent between the code you write in Rust SDK and Sway.