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
{
/// <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>
public readonly struct FormattedImage
public readonly struct FormattedImage : IEquatable<FormattedImage>
{
/// <summary>
/// Initializes a new instance of the <see cref="FormattedImage"/> struct.
/// </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>
public FormattedImage(Image image, IImageFormat format)
{
@ -34,41 +34,57 @@ namespace SixLabors.ImageSharp
public readonly IImageFormat Format { get; }
/// <summary>
/// Converts <see cref="FormattedImage"/> to <see cref="ValueTuple"/>
/// Converts <see cref="FormattedImage"/> to <see cref="ValueTuple"/>.
/// </summary>
/// <param name="value">The <see cref="FormattedImage"/> to convert.</param>
public static implicit operator (Image image, IImageFormat format)(FormattedImage value)
{
return (value.Image, value.Format);
}
=> (value.Image, value.Format);
/// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage"/>
/// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImage((Image image, IImageFormat format) value)
{
return new FormattedImage(value.image, value.format);
}
=> 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/>
public override bool Equals(object obj)
{
return obj is FormattedImage other &&
EqualityComparer<Image>.Default.Equals(this.Image, other.Image) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
=> obj is FormattedImage image && this.Equals(image);
/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(this.Image, this.Format);
}
public bool Equals(FormattedImage other)
=> EqualityComparer<Image>.Default.Equals(this.Image, other.Image)
&& EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Image, this.Format);
/// <summary>
/// Deconstructs <see cref="FormattedImage"/> into component parts.
/// </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>
public void Deconstruct(out Image image, out IImageFormat format)
{

46
src/ImageSharp/FormattedImageInfo.cs

@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Struct to curry <see cref="IImageInfo"/> and <see cref="IImageFormat"/> for return from async overloads.
/// </summary>
public readonly struct FormattedImageInfo
public readonly struct FormattedImageInfo : IEquatable<FormattedImageInfo>
{
/// <summary>
/// Initializes a new instance of the <see cref="FormattedImageInfo"/> struct.
@ -38,32 +38,46 @@ namespace SixLabors.ImageSharp
/// </summary>
/// <param name="value">The <see cref="FormattedImageInfo"/> to convert.</param>
public static implicit operator (IImageInfo imageInfo, IImageFormat format)(FormattedImageInfo value)
{
return (value.ImageInfo, value.Format);
}
=> (value.ImageInfo, value.Format);
/// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImageInfo"/>
/// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImageInfo((IImageInfo imageInfo, IImageFormat format) value)
{
return new FormattedImageInfo(value.imageInfo, value.format);
}
=> 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/>
public override bool Equals(object obj)
{
return obj is FormattedImageInfo other &&
EqualityComparer<IImageInfo>.Default.Equals(this.ImageInfo, other.ImageInfo) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
=> obj is FormattedImageInfo info && this.Equals(info);
/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(this.ImageInfo, this.Format);
}
public bool Equals(FormattedImageInfo other)
=> EqualityComparer<IImageInfo>.Default.Equals(this.ImageInfo, other.ImageInfo)
&& EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.ImageInfo, this.Format);
/// <summary>
/// Deconstructs <see cref="FormattedImageInfo"/> into component parts.

54
src/ImageSharp/FormattedImage{TPixel}.cs

@ -9,16 +9,16 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp
{
/// <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>
/// <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>
{
/// <summary>
/// Initializes a new instance of the <see cref="FormattedImage{TPixel}"/> struct.
/// </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>
public FormattedImage(Image<TPixel> image, IImageFormat format)
{
@ -41,37 +41,53 @@ namespace SixLabors.ImageSharp
/// </summary>
/// <param name="value">The <see cref="FormattedImage{TPixel}"/> to convert.</param>
public static implicit operator (Image<TPixel> image, IImageFormat format)(FormattedImage<TPixel> value)
{
return (value.Image, value.Format);
}
=> (value.Image, value.Format);
/// <summary>
/// Converts <see cref="ValueTuple"/> to <see cref="FormattedImage{TPixel}"/>
/// </summary>
/// <param name="value">The <see cref="ValueTuple"/> to convert.</param>
public static implicit operator FormattedImage<TPixel>((Image<TPixel> image, IImageFormat format) value)
{
return new FormattedImage<TPixel>(value.image, value.format);
}
=> 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/>
public override bool Equals(object obj)
{
return obj is FormattedImage<TPixel> other &&
EqualityComparer<Image<TPixel>>.Default.Equals(this.Image, other.Image) &&
EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
}
=> obj is FormattedImage<TPixel> image && this.Equals(image);
/// <inheritdoc/>
public override int GetHashCode()
{
return HashCode.Combine(this.Image, this.Format);
}
public bool Equals(FormattedImage<TPixel> other)
=> EqualityComparer<Image<TPixel>>.Default.Equals(this.Image, other.Image)
&& EqualityComparer<IImageFormat>.Default.Equals(this.Format, other.Format);
/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(this.Image, this.Format);
/// <summary>
/// Deconstructs <see cref="FormattedImage"/> into component parts.
/// </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>
public void Deconstruct(out Image<TPixel> image, out IImageFormat format)
{

Loading…
Cancel
Save