fn from()

in uniffi_bindgen/src/interface/ffi.rs [107:163]


    fn from(t: &Type) -> FfiType {
        match t {
            // Types that are the same map to themselves, naturally.
            Type::UInt8 => FfiType::UInt8,
            Type::Int8 => FfiType::Int8,
            Type::UInt16 => FfiType::UInt16,
            Type::Int16 => FfiType::Int16,
            Type::UInt32 => FfiType::UInt32,
            Type::Int32 => FfiType::Int32,
            Type::UInt64 => FfiType::UInt64,
            Type::Int64 => FfiType::Int64,
            Type::Float32 => FfiType::Float32,
            Type::Float64 => FfiType::Float64,
            // Booleans lower into an Int8, to work around a bug in JNA.
            Type::Boolean => FfiType::Int8,
            // Strings are always owned rust values.
            // We might add a separate type for borrowed strings in future.
            Type::String => FfiType::RustBuffer(None),
            // Byte strings are also always owned rust values.
            // We might add a separate type for borrowed byte strings in future as well.
            Type::Bytes => FfiType::RustBuffer(None),
            // Objects are pointers to an Arc<>
            Type::Object { name, .. } => FfiType::RustArcPtr(name.to_owned()),
            // Callback interfaces are passed as opaque integer handles.
            Type::CallbackInterface { .. } => FfiType::UInt64,
            // Other types are serialized into a bytebuffer and deserialized on the other side.
            Type::Enum { name, module_path } | Type::Record { name, module_path } => {
                FfiType::RustBuffer(Some(ExternalFfiMetadata {
                    name: name.clone(),
                    module_path: module_path.clone(),
                }))
            }
            Type::Optional { .. }
            | Type::Sequence { .. }
            | Type::Map { .. }
            | Type::Timestamp
            | Type::Duration => FfiType::RustBuffer(None),
            Type::Custom {
                builtin,
                name,
                module_path,
                ..
            } => {
                // We need ffitype of the builtin.
                match FfiType::from(builtin.as_ref()) {
                    // and if that builtin was a "local" RustBuffer, we need to
                    // let emit enough metadata so the bindings can call the rustbuffer impl
                    // if necessary.
                    FfiType::RustBuffer(None) => FfiType::RustBuffer(Some(ExternalFfiMetadata {
                        name: name.clone(),
                        module_path: module_path.clone(),
                    })),
                    t => t,
                }
            }
        }
    }