Repository: servo/servo
File: components/fonts/platform/macos/core_text_font_cache.rs (Rust)

Code before:
                    value: value as f32,
                }
            })
            .collect();

        if !modified_variations {
            return platform_font;
        }

        let variation_keys: Vec<_> = variations
            .iter()
            .map(|variation| CFNumber::new_i64(variation.tag as i64))
            .collect();
        let variation_values: Vec<_> = variations
            .iter()
            .map(|variation| CFNumber::new_f32(variation.value))
            .collect();
        let values_dict: CFRetained<CFDictionary<CFNumber, CFNumber>> = CFDictionary::from_slices(
            &variation_keys
                .iter()
                .map(CFRetained::as_ref)
                .collect::<Vec<_>>(),
            &variation_values
                .iter()
                .map(CFRetained::as_ref)
                .collect::<Vec<_>>(),
        );

        let attributes: CFRetained<CFDictionary<CFString, CFType>> = CFDictionary::from_slices(
            &[unsafe { kCTFontVariationAttribute }],
            &[values_dict.as_ref()],
        );
        let descriptor_with_variations = unsafe {
            platform_font
                .ctfont
                .font_descriptor()
                .copy_with_attributes(attributes.as_opaque())
        };

        let ctfont = unsafe {
            CTFont::with_font_descriptor(&descriptor_with_variations, pt_size, std::ptr::null())
        };
        PlatformFont::new_with_ctfont_and_variations(ctfont, variations, synthetic_bold)
    }

    fn get_variation_axis_information(
        platform_font: &PlatformFont,
    ) -> Option<Vec<VariationAxisInformation>> {
        let variation_axes = unsafe { platform_font.ctfont.variation_axes()? };
        let traits = unsafe { variation_axes.cast_unchecked::<CFDictionary<CFString, CFNumber>>() };


Review comment (suggestion):
Remark: Ideally, `objc2-core-foundation` would allow you to avoid these extra allocations (probably by having variants of `CFDictionary::from_slices` similar to `CFArray::from_retained_objects`).

Code after:
                    value: value as f32,
                }
            })
            .collect();

        if !modified_variations {
            return platform_font;
        }

        let variation_keys: Vec<_> = variations
            .iter()
            .map(|variation| CFNumber::new_i64(variation.tag as i64))
            .collect();
        let variation_values: Vec<_> = variations
            .iter()
            .map(|variation| CFNumber::new_f32(variation.value))
            .collect();
        let values_dict = CFDictionary::<CFNumber, CFNumber>::from_slices(
            &variation_keys
                .iter()
                .map(CFRetained::as_ref)
                .collect::<Vec<_>>(),
            &variation_values
                .iter()
                .map(CFRetained::as_ref)
                .collect::<Vec<_>>(),
        );

        let attributes = CFDictionary::<CFString, CFType>::from_slices(
            &[unsafe { kCTFontVariationAttribute }],
            &[values_dict.as_ref()],
        );
        let descriptor_with_variations = unsafe {
            platform_font
                .ctfont
                .font_descriptor()
                .copy_with_attributes(attributes.as_opaque())
        };

        let ctfont = unsafe {
            CTFont::with_font_descriptor(&descriptor_with_variations, pt_size, std::ptr::null())
        };
        PlatformFont::new_with_ctfont_and_variations(ctfont, variations, synthetic_bold)
    }

    fn get_variation_axis_information(
        platform_font: &PlatformFont,
    ) -> Option<Vec<VariationAxisInformation>> {
        let variation_axes = unsafe { platform_font.ctfont.variation_axes()? };
        let traits = unsafe { variation_axes.cast_unchecked::<CFDictionary<CFString, CFNumber>>() };