Repository: aptos-labs/aptos-core
File: api/types/src/bytecode.rs (Rust)

Code before:
            },
            SignatureToken::TypeParameter(i) => MoveType::GenericTypeParam { index: *i },
            SignatureToken::Reference(t) => MoveType::Reference {
                mutable: false,
                to: Box::new(self.new_move_type(t.borrow())),
            },
            SignatureToken::MutableReference(t) => MoveType::Reference {
                mutable: true,
                to: Box::new(self.new_move_type(t.borrow())),
            },
            SignatureToken::Function(args, result, abilities) => {
                let new_vec = |toks: &[SignatureToken]| {
                    toks.iter()
                        .map(|t| self.new_move_type(t))
                        .collect::<Vec<_>>()
                };
                MoveType::Function {
                    args: new_vec(args),
                    results: new_vec(result),
                    abilities: *abilities,
                }
            },
        }
    }

    fn new_move_struct(&self, def: &StructDefinition, mod_of_def: &ModuleId) -> MoveStruct {
        let handle = self.struct_handle_at(def.struct_handle);
        let mut is_enum = self.struct_is_enum(def);
        let (is_native, fields) = match &def.field_information {
            StructFieldInformation::Native => (true, vec![]),
            StructFieldInformation::Declared(fields) => (
                false,
                fields
                    .iter()
                    .map(|f| self.new_move_struct_field(f))
                    .collect(),
            ),
            StructFieldInformation::DeclaredVariants(variant_defs) => {
                // Special case for enum representation of option module
                if mod_of_def.is_option() && variant_defs.len() == 2 {
                    is_enum = false;
                    let variant = &variant_defs[1];
                    let field_type = MoveType::Vector {
                        items: Box::new(self.new_move_type(&variant.fields[0].signature.0)),
                    };
                    let field = MoveStructField {
                        name: Identifier::new(LEGACY_OPTION_VEC).unwrap().into(),
                        typ: field_type,
                    };
                    (false, vec![field])
                } else {

Review comment (suggestion):
Can't we get module ID out of handle below on the first line?Seems a bit weird to pass this separately if we can access it via self.get_module_id(def) or so

Code after:
            },
            SignatureToken::TypeParameter(i) => MoveType::GenericTypeParam { index: *i },
            SignatureToken::Reference(t) => MoveType::Reference {
                mutable: false,
                to: Box::new(self.new_move_type(t.borrow())),
            },
            SignatureToken::MutableReference(t) => MoveType::Reference {
                mutable: true,
                to: Box::new(self.new_move_type(t.borrow())),
            },
            SignatureToken::Function(args, result, abilities) => {
                let new_vec = |toks: &[SignatureToken]| {
                    toks.iter()
                        .map(|t| self.new_move_type(t))
                        .collect::<Vec<_>>()
                };
                MoveType::Function {
                    args: new_vec(args),
                    results: new_vec(result),
                    abilities: *abilities,
                }
            },
        }
    }

    fn new_move_struct(&self, def: &StructDefinition) -> MoveStruct {
        let handle = self.struct_handle_at(def.struct_handle);
        let mut is_enum = self.struct_is_enum(def);
        let (is_native, fields) = match &def.field_information {
            StructFieldInformation::Native => (true, vec![]),
            StructFieldInformation::Declared(fields) => (
                false,
                fields
                    .iter()
                    .map(|f| self.new_move_struct_field(f))
                    .collect(),
            ),
            StructFieldInformation::DeclaredVariants(variant_defs) => {
                // For `Option`, we want to keep it backwards compatible with the old representation
                // instead of enum representation, we convert it to a struct with `vec` field.
                let module_handle = self.module_handle_at(handle.module);
                let mid = ModuleId {
                    address: *self.address_identifier_at(module_handle.address),
                    name: self.identifier_at(module_handle.name).to_owned(),
                };
                if mid.is_option() && variant_defs.len() == 2 {
                    is_enum = false;
                    let variant = &variant_defs[1];
                    let field_type = MoveType::Vector {
                        items: Box::new(self.new_move_type(&variant.fields[0].signature.0)),
                    };