Repository: servo/servo
File: components/fonts/platform/freetype/font_list.rs (Rust)

Code before:
                        log::error!(
                            "Ignoring font family because its name contains invalid UTF-8: {error:?}"
                        );
                        continue;
                    },
                };
                callback(family_name.to_owned());
                v += 1;
            }
        }
    }
}

pub(crate) fn for_each_variation<F>(family_name: &str, mut callback: F)
where
    F: FnMut(FontTemplate),
{
    unsafe {
        let config = FcConfigGetCurrent();
        let mut font_set = FcConfigGetFonts(config, FcSetSystem);
        let font_set_array_ptr = &mut font_set;
        let pattern = FcPatternCreate();
        if pattern.is_null() {
            log::error!("Failed to create FcPattern");
            return;
        }

        let Ok(family_name_cstr) = CString::new(family_name) else {
            return;
        };
        let ok = FcPatternAddString(
            pattern,
            FC_FAMILY.as_ptr() as *mut c_char,
            family_name_cstr.as_ptr() as *const FcChar8,
        );
        if ok == 0 {
            log::error!("Failed to create FcPattern");
            return;
        }

        let object_set = FcObjectSetCreate();
        if object_set.is_null() {
            log::error!("Failed to create FcObjectSet");
            return;
        }

        FcObjectSetAdd(object_set, FC_FILE.as_ptr() as *mut c_char);
        FcObjectSetAdd(object_set, FC_INDEX.as_ptr() as *mut c_char);
        FcObjectSetAdd(object_set, FC_WEIGHT.as_ptr() as *mut c_char);
        FcObjectSetAdd(object_set, FC_SLANT.as_ptr() as *mut c_char);
        FcObjectSetAdd(object_set, FC_WIDTH.as_ptr() as *mut c_char);

Review comment (bug):
I think for cases like in this function, we might want to have a macro that does a debug_assert, and something else (like return here) when debug-assertions are disabled. The reason being that if something is just an error message, it is much more likely to go unnoticed / unfixed, if everything else "still works". In the long term this can lead to many subtle issues.

Code after:
                        log::error!(
                            "Ignoring font family because its name contains invalid UTF-8: {error:?}"
                        );
                        continue;
                    },
                };
                callback(family_name.to_owned());
                v += 1;
            }
        }
    }
}

pub(crate) fn for_each_variation<F>(family_name: &str, mut callback: F)
where
    F: FnMut(FontTemplate),
{
    unsafe {
        let config = FcConfigGetCurrent();
        let mut font_set = FcConfigGetFonts(config, FcSetSystem);
        let font_set_array_ptr = &mut font_set;
        let pattern = FcPatternCreate();
        debug_assert!(!pattern.is_null());
        if pattern.is_null() {
            log::error!("Failed to create FcPattern");
            return;
        }

        let Ok(family_name_cstr) = CString::new(family_name) else {
            return;
        };
        let ok = FcPatternAddString(
            pattern,
            FC_FAMILY.as_ptr() as *mut c_char,
            family_name_cstr.as_ptr() as *const FcChar8,
        );
        debug_assert_ne!(ok, 0);
        if ok == 0 {
            log::error!("Failed to create FcPattern");
            return;
        }

        let object_set = FcObjectSetCreate();
        debug_assert!(!object_set.is_null());
        if object_set.is_null() {
            log::error!("Failed to create FcObjectSet");
            return;
        }

        FcObjectSetAdd(object_set, FC_FILE.as_ptr() as *mut c_char);
        FcObjectSetAdd(object_set, FC_INDEX.as_ptr() as *mut c_char);