Browse Source

implement IEquatable<T>

pull/1574/head
James Jackson-South 6 years ago
parent
commit
45ad33f2ae
  1. 56
      src/ImageSharp/FormattedImage.cs
  2. 46
      src/ImageSharp/FormattedImageInfo.cs
  3. 54
      src/ImageSharp/FormattedImage{TPixel}.cs

56
src/ImageSharp/FormattedImage.cs

@ -8,14 +8,14 @@ using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
{ {
/// <summary> /// <summary>
/// Struct to curry <see cref="ImageSharp.Image"/> and <see cref="IImageFormat"/> for return from async overloads. /// Struct to curry <see cref="Image"/> and <see cref="IImageFormat"/> for return from async overloads.
/// </summary> /// </summary>
public readonly struct FormattedImage public readonly struct FormattedImage : IEquatable<FormattedImage>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FormattedImage"/> struct. /// Initializes a new instance of the <see cref="FormattedImage"/> struct.
/// </summary> /// </summary>
/// <param name="image">The <see cref="FormattedImage"/>.</param> /// <param name="image">The <see cref="Image"/>.</param>
/// <param name="format">The <see cref="IImageFormat"/>.</param> /// <param name="format">The <see cref="IImageFormat"/>.</param>
public FormattedImage(Image image, IImageFormat format) public FormattedImage(Image image, IImageFormat format)
{ {
@ -34,41 +34,57 @@ namespace SixLabors.ImageSharp
public readonly IImageFormat Format { get; } public readonly IImageFormat Format { get; }
/// <summary> /// <summary>
/// Converts <see cref="FormattedImage"/> to <see cref="ValueTuple"/> /// Converts <see cref="FormattedImage"/> to <see cref="ValueTuple"/>.
/// </summary> /// </summary>
/// <param name="value">The <see cref="FormattedImage"/> to convert.</param> /// <param name="value">The <see cref="FormattedImage"/> to convert.</param>
public static implicit operator (Image image, IImageFormat format)(FormattedImage value) public static implicit operator (Image image, IImageFormat format)(FormattedImage value)
{ => (value.Image, value.Format);
return (value.Image, value.Format);
}
/// <summary> /// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage"/> /// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage"/>
/// </summary> /// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param> /// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImage((Image image, IImageFormat format) value) public static implicit operator FormattedImage((Image image, IImageFormat format) value)
{ => new FormattedImage(value.image, value.format);
return new FormattedImage(value.image, value.format);
} /// <summary>
/// Compares two <see cref="FormattedImage"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="FormattedImage"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImage"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(FormattedImage left, FormattedImage right)
=> left.Equals(right);
/// <summary>
/// Compares two <see cref="FormattedImage"/> objects for inequality.
/// </summary>
/// <param name="left">The <see cref="FormattedImage"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImage"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(FormattedImage left, FormattedImage right)
=> !(left == right);
/// <inheritdoc/> /// <inheritdoc/>
public override bool Equals(object obj) public override bool Equals(object obj)
{ => obj is FormattedImage image && this.Equals(image);
return obj is FormattedImage other &&
EqualityComparer<Image>.Default.Equals(this.Image, other.Image) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/> /// <inheritdoc/>
public override int GetHashCode() public bool Equals(FormattedImage other)
{ => EqualityComparer<Image>.Default.Equals(this.Image, other.Image)
return HashCode.Combine(this.Image, this.Format); && EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Image, this.Format);
/// <summary> /// <summary>
/// Deconstructs <see cref="FormattedImage"/> into component parts. /// Deconstructs <see cref="FormattedImage"/> into component parts.
/// </summary> /// </summary>
/// <param name="image">The <see cref="ImageSharp.Image"/>.</param> /// <param name="image">The <see cref="Image"/>.</param>
/// <param name="format">The <see cref="IImageFormat"/>.</param> /// <param name="format">The <see cref="IImageFormat"/>.</param>
public void Deconstruct(out Image image, out IImageFormat format) public void Deconstruct(out Image image, out IImageFormat format)
{ {

46
src/ImageSharp/FormattedImageInfo.cs

@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp
/// <summary> /// <summary>
/// Struct to curry <see cref="IImageInfo"/> and <see cref="IImageFormat"/> for return from async overloads. /// Struct to curry <see cref="IImageInfo"/> and <see cref="IImageFormat"/> for return from async overloads.
/// </summary> /// </summary>
public readonly struct FormattedImageInfo public readonly struct FormattedImageInfo : IEquatable<FormattedImageInfo>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FormattedImageInfo"/> struct. /// Initializes a new instance of the <see cref="FormattedImageInfo"/> struct.
@ -38,32 +38,46 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="value">The <see cref="FormattedImageInfo"/> to convert.</param> /// <param name="value">The <see cref="FormattedImageInfo"/> to convert.</param>
public static implicit operator (IImageInfo imageInfo, IImageFormat format)(FormattedImageInfo value) public static implicit operator (IImageInfo imageInfo, IImageFormat format)(FormattedImageInfo value)
{ => (value.ImageInfo, value.Format);
return (value.ImageInfo, value.Format);
}
/// <summary> /// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImageInfo"/> /// Converts <see cref="ValueTuple"/> to <see cref="FormattedImageInfo"/>
/// </summary> /// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param> /// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImageInfo((IImageInfo imageInfo, IImageFormat format) value) public static implicit operator FormattedImageInfo((IImageInfo imageInfo, IImageFormat format) value)
{ => new FormattedImageInfo(value.imageInfo, value.format);
return new FormattedImageInfo(value.imageInfo, value.format);
} /// <summary>
/// Compares two <see cref="FormattedImageInfo"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="FormattedImageInfo"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImageInfo"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(FormattedImageInfo left, FormattedImageInfo right) => left.Equals(right);
/// <summary>
/// Compares two <see cref="FormattedImageInfo"/> objects for inequality.
/// </summary>
/// <param name="left">The <see cref="FormattedImageInfo"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImageInfo"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(FormattedImageInfo left, FormattedImageInfo right) => !(left == right);
/// <inheritdoc/> /// <inheritdoc/>
public override bool Equals(object obj) public override bool Equals(object obj)
{ => obj is FormattedImageInfo info && this.Equals(info);
return obj is FormattedImageInfo other &&
EqualityComparer<IImageInfo>.Default.Equals(this.ImageInfo, other.ImageInfo) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/> /// <inheritdoc/>
public override int GetHashCode() public bool Equals(FormattedImageInfo other)
{ => EqualityComparer<IImageInfo>.Default.Equals(this.ImageInfo, other.ImageInfo)
return HashCode.Combine(this.ImageInfo, this.Format); && EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.ImageInfo, this.Format);
/// <summary> /// <summary>
/// Deconstructs <see cref="FormattedImageInfo"/> into component parts. /// Deconstructs <see cref="FormattedImageInfo"/> into component parts.

54
src/ImageSharp/FormattedImage{TPixel}.cs

@ -9,16 +9,16 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
{ {
/// <summary> /// <summary>
/// Struct to curry <see cref="ImageSharp.Image{TPixel}"/> and <see cref="IImageFormat"/> for return from async overloads. /// Struct to curry <see cref="Image{TPixel}"/> and <see cref="IImageFormat"/> for return from async overloads.
/// </summary> /// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
public readonly struct FormattedImage<TPixel> public readonly struct FormattedImage<TPixel> : IEquatable<FormattedImage<TPixel>>
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="FormattedImage{TPixel}"/> struct. /// Initializes a new instance of the <see cref="FormattedImage{TPixel}"/> struct.
/// </summary> /// </summary>
/// <param name="image">The <see cref="ImageSharp.Image{TPixel}"/>.</param> /// <param name="image">The <see cref="Image{TPixel}"/>.</param>
/// <param name="format">The <see cref="IImageFormat"/>.</param> /// <param name="format">The <see cref="IImageFormat"/>.</param>
public FormattedImage(Image<TPixel> image, IImageFormat format) public FormattedImage(Image<TPixel> image, IImageFormat format)
{ {
@ -41,37 +41,53 @@ namespace SixLabors.ImageSharp
/// </summary> /// </summary>
/// <param name="value">The <see cref="FormattedImage{TPixel}"/> to convert.</param> /// <param name="value">The <see cref="FormattedImage{TPixel}"/> to convert.</param>
public static implicit operator (Image<TPixel> image, IImageFormat format)(FormattedImage<TPixel> value) public static implicit operator (Image<TPixel> image, IImageFormat format)(FormattedImage<TPixel> value)
{ => (value.Image, value.Format);
return (value.Image, value.Format);
}
/// <summary> /// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage{TPixel}"/> /// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage{TPixel}"/>
/// </summary> /// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param> /// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImage<TPixel>((Image<TPixel> image, IImageFormat format) value) public static implicit operator FormattedImage<TPixel>((Image<TPixel> image, IImageFormat format) value)
{ => new FormattedImage<TPixel>(value.image, value.format);
return new FormattedImage<TPixel>(value.image, value.format);
} /// <summary>
/// Compares two <see cref="FormattedImage{TPixel}"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="FormattedImage{TPixel}"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImage{TPixel}"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(FormattedImage<TPixel> left, FormattedImage<TPixel> right)
=> left.Equals(right);
/// <summary>
/// Compares two <see cref="FormattedImage{TPixel}"/> objects for inequality.
/// </summary>
/// <param name="left">The <see cref="FormattedImage{TPixel}"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="FormattedImage{TPixel}"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(FormattedImage<TPixel> left, FormattedImage<TPixel> right)
=> !(left == right);
/// <inheritdoc/> /// <inheritdoc/>
public override bool Equals(object obj) public override bool Equals(object obj)
{ => obj is FormattedImage<TPixel> image && this.Equals(image);
return obj is FormattedImage<TPixel> other &&
EqualityComparer<Image<TPixel>>.Default.Equals(this.Image, other.Image) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/> /// <inheritdoc/>
public override int GetHashCode() public bool Equals(FormattedImage<TPixel> other)
{ => EqualityComparer<Image<TPixel>>.Default.Equals(this.Image, other.Image)
return HashCode.Combine(this.Image, this.Format); && EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Image, this.Format);
/// <summary> /// <summary>
/// Deconstructs <see cref="FormattedImage"/> into component parts. /// Deconstructs <see cref="FormattedImage"/> into component parts.
/// </summary> /// </summary>
/// <param name="image">The <see cref="ImageSharp.Image{TPixel}"/>.</param> /// <param name="image">The <see cref="Image{TPixel}"/>.</param>
/// <param name="format">The <see cref="IImageFormat"/>.</param> /// <param name="format">The <see cref="IImageFormat"/>.</param>
public void Deconstruct(out Image<TPixel> image, out IImageFormat format) public void Deconstruct(out Image<TPixel> image, out IImageFormat format)
{ {

Loading…
Cancel
Save