Repository: bevyengine/bevy
File: crates/bevy_image/src/image.rs (Rust)

Code before:
        value.texture_descriptor.format = format;
        value.texture_descriptor.dimension = dimension;
        value.asset_usage = asset_usage;
        value.resize(size);

        debug_assert_eq!(
            pixel.len() % format.pixel_size(),
            0,
            "Must not have incomplete pixel data (pixel size is {}B).",
            format.pixel_size(),
        );
        debug_assert!(
            pixel.len() <= value.data.len(),
            "Fill data must fit within pixel buffer (expected {}B).",
            value.data.len(),
        );

        for current_pixel in value.data.chunks_exact_mut(pixel.len()) {
            current_pixel.copy_from_slice(pixel);
        }
        value
    }

    /// Create a new zero-filled image with a given size, which can be rendered to.
    /// This is primarily for use as a render target for a [`Camera`](`bevy_render::camera::Camera`).
    /// See [`RenderTarget::Image`](`bevy_render::camera::RenderTarget::Image`).
    pub fn new_target_texture(width: u32, height: u32) -> Self {
        let mut image = Self::default();

        image.resize(Extent3d {
            width,
            height,
            ..Default::default()
        });

        // You need to set these texture usage flags in order to use the image as a render target
        image.texture_descriptor.usage = TextureUsages::TEXTURE_BINDING
            | TextureUsages::COPY_DST
            | TextureUsages::RENDER_ATTACHMENT;

        image
    }

    /// Returns the width of a 2D image.
    #[inline]
    pub fn width(&self) -> u32 {
        self.texture_descriptor.size.width
    }

    /// Returns the height of a 2D image.
    #[inline]

Review comment (suggestion):
This fixes those pesky doc links :)

```suggestion
  /// Create a new zero-filled image with a given size, which can be rendered to. This is primarily
  /// for use as a render target for a [`Camera`]. See [`RenderTarget::Image`].
  ///
  /// [`Camera`]: https://docs.rs/bevy/latest/bevy/render/camera/struct.Camera.html
  /// [`RenderTarget::Image`]: https://docs.rs/bevy/latest/bevy/render/camera/enum.RenderTarget.html#variant.Image
```

Code after:
    ///
    /// # Panics
    /// Panics if the length of the `data`, volume of the `size` and the size of the `format`
    /// do not match.
    pub fn new(
        size: Extent3d,
        dimension: TextureDimension,
        data: Vec<u8>,
        format: TextureFormat,
        asset_usage: RenderAssetUsages,
    ) -> Self {
        debug_assert_eq!(
            size.volume() * format.pixel_size(),
            data.len(),
            "Pixel data, size and format have to match",
        );
        let mut image = Image::new_uninit(size, dimension, format, asset_usage);
        image.data = Some(data);
        image
    }

    /// Exactly the same as [`Image::new`], but doesn't initialize the image
    pub fn new_uninit(
        size: Extent3d,
        dimension: TextureDimension,
        format: TextureFormat,
        asset_usage: RenderAssetUsages,
    ) -> Self {
        Image {
            data: None,
            data_order: TextureDataOrder::default(),
            texture_descriptor: TextureDescriptor {
                size,
                format,
                dimension,
                label: None,
                mip_level_count: 1,
                sample_count: 1,
                usage: TextureUsages::TEXTURE_BINDING
                    | TextureUsages::COPY_DST
                    | TextureUsages::COPY_SRC,
                view_formats: &[],
            },
            sampler: ImageSampler::Default,
            texture_view_descriptor: None,
            asset_usage,
            copy_on_resize: false,
        }
    }

    /// A transparent white 1x1x1 image.