Repository: servo/servo
File: components/layout/replaced.rs (Rust)

Code before:
        let transfer = |size| ratio.compute_dependent_size(axis, size);
        match get_size_in_opposite_axis() {
            SizeConstraint::Definite(size) => transfer(size),
            SizeConstraint::MinMax(min_size, max_size) => get_fallback_size()
                .clamp_between_extremums(transfer(min_size), max_size.map(transfer)),
        }
    }

    fn calculate_fragment_rect(
        &self,
        style: &ServoArc<ComputedValues>,
        size: PhysicalSize<Au>,
    ) -> (PhysicalSize<Au>, PhysicalRect<Au>) {
        if let ReplacedContentKind::Image(ImageInfo {
            image,
            showing_broken_image_icon: true,
        }) = &self.kind
        {
            let (width, height) = match &image {
                Some(Image::Raster(image)) => {
                    (image.metadata.width as f32, image.metadata.height as f32)
                },
                Some(Image::Vector(image)) => {
                    (image.metadata.width as f32, image.metadata.height as f32)
                },
                None => (0., 0.),
            };
            let size = Size2D::new(Au::from_f32_px(width), Au::from_f32_px(height)).min(size);
            return (PhysicalSize::zero(), size.into());
        }

        let natural_size = PhysicalSize::new(
            self.natural_size.width.unwrap_or(size.width),
            self.natural_size.height.unwrap_or(size.height),
        );

        let object_fit_size = self.natural_size.ratio.map_or(size, |width_over_height| {
            let preserve_aspect_ratio_with_comparison =
                |size: PhysicalSize<Au>, comparison: fn(&Au, &Au) -> bool| {
                    let candidate_width = size.height.scale_by(width_over_height);
                    if comparison(&candidate_width, &size.width) {
                        return PhysicalSize::new(candidate_width, size.height);
                    }

                    let candidate_height = size.width.scale_by(1. / width_over_height);
                    debug_assert!(comparison(&candidate_height, &size.height));
                    PhysicalSize::new(size.width, candidate_height)
                };

            match style.clone_object_fit() {
                ObjectFit::Fill => size,

Review comment (suggestion):
Let me know if want to break this into seperate PR or remove for `Image::Vector`, as it considers it now 

Code after:
        let transfer = |size| ratio.compute_dependent_size(axis, size);
        match get_size_in_opposite_axis() {
            SizeConstraint::Definite(size) => transfer(size),
            SizeConstraint::MinMax(min_size, max_size) => get_fallback_size()
                .clamp_between_extremums(transfer(min_size), max_size.map(transfer)),
        }
    }

    fn calculate_fragment_rect(
        &self,
        style: &ServoArc<ComputedValues>,
        size: PhysicalSize<Au>,
    ) -> (PhysicalSize<Au>, PhysicalRect<Au>) {
        if let ReplacedContentKind::Image(ImageInfo {
            image: Some(Image::Raster(image)),
            showing_broken_image_icon: true,
        }) = &self.kind
        {
            let size = Size2D::new(
                Au::from_f32_px(image.metadata.width as f32),
                Au::from_f32_px(image.metadata.height as f32),
            )
            .min(size);
            return (PhysicalSize::zero(), size.into());
        }

        let natural_size = PhysicalSize::new(
            self.natural_size.width.unwrap_or(size.width),
            self.natural_size.height.unwrap_or(size.height),
        );

        let object_fit_size = self.natural_size.ratio.map_or(size, |width_over_height| {
            let preserve_aspect_ratio_with_comparison =
                |size: PhysicalSize<Au>, comparison: fn(&Au, &Au) -> bool| {
                    let candidate_width = size.height.scale_by(width_over_height);
                    if comparison(&candidate_width, &size.width) {
                        return PhysicalSize::new(candidate_width, size.height);
                    }

                    let candidate_height = size.width.scale_by(1. / width_over_height);
                    debug_assert!(comparison(&candidate_height, &size.height));
                    PhysicalSize::new(size.width, candidate_height)
                };

            match style.clone_object_fit() {
                ObjectFit::Fill => size,
                ObjectFit::Contain => preserve_aspect_ratio_with_comparison(size, PartialOrd::le),
                ObjectFit::Cover => preserve_aspect_ratio_with_comparison(size, PartialOrd::ge),
                ObjectFit::None => natural_size,
                ObjectFit::ScaleDown => {
                    preserve_aspect_ratio_with_comparison(size.min(natural_size), PartialOrd::le)