magnus/r_rational.rs
1use std::{fmt, num::NonZeroI64};
2
3use rb_sys::{rb_rational_den, rb_rational_new, rb_rational_num, ruby_value_type, VALUE};
4
5use crate::{
6 error::Error,
7 integer::Integer,
8 into_value::IntoValue,
9 numeric::Numeric,
10 try_convert::TryConvert,
11 value::{
12 private::{self, ReprValue as _},
13 NonZeroValue, ReprValue, Value,
14 },
15 Ruby,
16};
17
18/// # `RRational`
19///
20/// Functions that can be used to create Ruby `Rational`s.
21///
22/// See also the [`RRational`] type.
23impl Ruby {
24 /// Create a new `RRational`.
25 ///
26 /// # Examples
27 ///
28 /// ```
29 /// use std::num::NonZeroI64;
30 ///
31 /// use magnus::{Error, Ruby};
32 ///
33 /// fn example(ruby: &Ruby) -> Result<(), Error> {
34 /// let rational = ruby.rational_new(2, NonZeroI64::new(4).unwrap());
35 /// assert_eq!(rational.to_string(), "1/2");
36 ///
37 /// Ok(())
38 /// }
39 /// # Ruby::init(example).unwrap()
40 /// ```
41 pub fn rational_new(&self, num: i64, den: NonZeroI64) -> RRational {
42 let num = self.into_value(num);
43 let den = self.into_value(den.get());
44 unsafe {
45 RRational::from_rb_value_unchecked(rb_rational_new(
46 num.as_rb_value(),
47 den.as_rb_value(),
48 ))
49 }
50 }
51}
52
53/// A Value pointer to a RRational struct, Ruby's internal representation of
54/// rational numbers.
55///
56/// See the [`ReprValue`] trait for additional methods available on this type.
57/// See [`Ruby`](Ruby#rrational) for methods to create an `RRational`.
58#[derive(Clone, Copy)]
59#[repr(transparent)]
60pub struct RRational(NonZeroValue);
61
62impl RRational {
63 /// Return `Some(RRational)` if `val` is a `RRational`, `None` otherwise.
64 ///
65 /// # Examples
66 ///
67 /// ```
68 /// use magnus::{eval, RRational};
69 /// # let _cleanup = unsafe { magnus::embed::init() };
70 ///
71 /// assert!(RRational::from_value(eval("1/2r").unwrap()).is_some());
72 /// assert!(RRational::from_value(eval("0.5").unwrap()).is_none());
73 /// ```
74 #[inline]
75 pub fn from_value(val: Value) -> Option<Self> {
76 unsafe {
77 (val.rb_type() == ruby_value_type::RUBY_T_RATIONAL)
78 .then(|| Self(NonZeroValue::new_unchecked(val)))
79 }
80 }
81
82 #[inline]
83 pub(crate) unsafe fn from_rb_value_unchecked(val: VALUE) -> Self {
84 Self(NonZeroValue::new_unchecked(Value::new(val)))
85 }
86
87 /// Create a new `RRational`.
88 ///
89 /// # Panics
90 ///
91 /// Panics if called from a non-Ruby thread. See [`Ruby::rational_new`] for
92 /// the non-panicking version.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// # #![allow(deprecated)]
98 /// use std::num::NonZeroI64;
99 ///
100 /// use magnus::RRational;
101 /// # let _cleanup = unsafe { magnus::embed::init() };
102 ///
103 /// let rational = RRational::new(2, NonZeroI64::new(4).unwrap());
104 /// assert_eq!(rational.to_string(), "1/2");
105 /// ```
106 #[cfg_attr(
107 not(feature = "old-api"),
108 deprecated(note = "please use `Ruby::rational_new` instead")
109 )]
110 #[cfg_attr(docsrs, doc(cfg(feature = "old-api")))]
111 #[inline]
112 pub fn new(num: i64, den: NonZeroI64) -> Self {
113 get_ruby!().rational_new(num, den)
114 }
115
116 /// Returns `self`'s numerator.
117 ///
118 /// # Examples
119 ///
120 /// ```
121 /// use std::num::NonZeroI64;
122 ///
123 /// use magnus::{Error, Ruby};
124 ///
125 /// fn example(ruby: &Ruby) -> Result<(), Error> {
126 /// let rational = ruby.rational_new(6, NonZeroI64::new(9).unwrap());
127 /// assert_eq!(rational.num().to_i64()?, 2);
128 ///
129 /// Ok(())
130 /// }
131 /// # Ruby::init(example).unwrap()
132 /// ```
133 pub fn num(self) -> Integer {
134 unsafe { Integer::from_rb_value_unchecked(rb_rational_num(self.as_rb_value())) }
135 }
136
137 /// Returns `self`'s denominator.
138 ///
139 /// # Examples
140 ///
141 /// ```
142 /// use std::num::NonZeroI64;
143 ///
144 /// use magnus::{Error, Ruby};
145 ///
146 /// fn example(ruby: &Ruby) -> Result<(), Error> {
147 /// let rational = ruby.rational_new(6, NonZeroI64::new(9).unwrap());
148 /// assert_eq!(rational.den().to_i64()?, 3);
149 ///
150 /// Ok(())
151 /// }
152 /// # Ruby::init(example).unwrap()
153 /// ```
154 pub fn den(self) -> Integer {
155 unsafe { Integer::from_rb_value_unchecked(rb_rational_den(self.as_rb_value())) }
156 }
157}
158
159impl fmt::Display for RRational {
160 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161 write!(f, "{}", unsafe { self.to_s_infallible() })
162 }
163}
164
165impl fmt::Debug for RRational {
166 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
167 write!(f, "{}", self.inspect())
168 }
169}
170
171impl IntoValue for RRational {
172 #[inline]
173 fn into_value_with(self, _: &Ruby) -> Value {
174 self.0.get()
175 }
176}
177
178unsafe impl private::ReprValue for RRational {}
179
180impl Numeric for RRational {}
181
182impl ReprValue for RRational {}
183
184impl TryConvert for RRational {
185 fn try_convert(val: Value) -> Result<Self, Error> {
186 Self::from_value(val).ok_or_else(|| {
187 Error::new(
188 Ruby::get_with(val).exception_type_error(),
189 format!("no implicit conversion of {} into Rational", unsafe {
190 val.classname()
191 },),
192 )
193 })
194 }
195}