Hone logo
Hone
Problems

Implementing the TryFrom Trait for Safe Type Conversions

The TryFrom trait in Rust provides a safe and ergonomic way to attempt converting one type into another. It allows you to define how a conversion should happen, handling potential errors gracefully instead of panicking at runtime. This challenge asks you to implement the TryFrom trait for a custom data structure, demonstrating your understanding of error handling and type conversions in Rust.

Problem Description

You are given a struct Point representing a 2D point with x and y coordinates, both of type i32. You are also given a struct Polar representing a point in polar coordinates, with radius (f64) and angle (f64, in radians). Your task is to implement the TryFrom<Point, E> trait, where E is a custom error type PolarConversionError.

The conversion from Point to Polar involves calculating the radius and angle. The radius is the Euclidean distance from the origin (0, 0) to the point (x, y). The angle is the angle in radians between the positive x-axis and the line connecting the origin to the point.

The TryFrom implementation should return a Polar struct if the conversion is successful. If either the x or y coordinate of the Point is i32::MIN, the conversion should fail and return a PolarConversionError::CoordinateOverflow error. This is to prevent potential overflow issues when calculating the radius.

Key Requirements:

  • Implement the TryFrom<Point, PolarConversionError> trait.
  • Handle potential overflow errors when calculating the radius.
  • Return a Polar struct on successful conversion.
  • Return a PolarConversionError on failure.

Expected Behavior:

  • For valid Point values (where x and y are not i32::MIN), the conversion should calculate the radius and angle correctly and return a Polar struct.
  • If either x or y is i32::MIN, the conversion should return a PolarConversionError::CoordinateOverflow error.

Examples

Example 1:

Input: Point { x: 3, y: 4 }
Output: Polar { radius: 5.0, angle: 0.9272952180016122 }
Explanation: Radius = sqrt(3^2 + 4^2) = 5.0, Angle = atan(4/3) ≈ 0.927 radians.

Example 2:

Input: Point { x: 0, y: 0 }
Output: Polar { radius: 0.0, angle: 0.0 }
Explanation: Radius = sqrt(0^2 + 0^2) = 0.0, Angle = atan(0/0) is defined as 0.0.

Example 3:

Input: Point { x: i32::MIN, y: 1 }
Output: PolarConversionError::CoordinateOverflow
Explanation: x is i32::MIN, which triggers the overflow error.

Constraints

  • Point has x and y fields of type i32.
  • Polar has radius field of type f64 and angle field of type f64.
  • The angle should be calculated in radians.
  • The radius calculation should avoid overflow.
  • The PolarConversionError enum should have a variant CoordinateOverflow.

Notes

  • You'll need to use the std::convert::TryFrom trait and the Result type.
  • Consider using the f64::atan() function for angle calculation.
  • The f64::sqrt() function is useful for calculating the radius.
  • Think carefully about how to handle the edge case where x and y are both zero to avoid division by zero errors when calculating the angle. You can define atan(0) as 0.0.
  • Remember to handle the overflow condition correctly. Using checked_add and checked_mul can help prevent overflow during radius calculation.
  • The TryFrom trait requires you to define an error type. Create a custom enum PolarConversionError for this purpose.
Loading editor...
rust