#![feature(specialization)] #[macro_use] extern crate pyo3; use pyo3::prelude::*; #[pyclass(module = "unit_converter")] struct Temperature { celsius: f32, } #[pymethods] impl Temperature { #[new] fn new(obj: &PyRawObject, temperature: f32) { obj.init(Temperature { celsius: temperature, }); } fn to_fahrenheit(&self) -> f32 { self.celsius * 1.8 + 32.0 } fn windchill(&self, wind_speed_kph: f32) -> f32 { 13.12 + (0.6215 * self.celsius) - (11.37 * wind_speed_kph.powf(0.16)) + (0.3965 * self.celsius * wind_speed_kph.powf(0.16)) } } // TODO: Add a test that -20 C and 32 kph feels like -32.9 #[pyfunction] fn convert_to_fahrenheit(celsius: f32) -> f32 { celsius * 1.8 + 32.0 } #[pyfunction] fn batch_convert_to_fahrenheit(celsius: Vec) -> Vec { celsius .iter() .map(|temperature| convert_to_fahrenheit(temperature.clone())) .collect() } #[pymodule] fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> { m.add_wrapped(wrap_pyfunction!(convert_to_fahrenheit))?; m.add_wrapped(wrap_pyfunction!(batch_convert_to_fahrenheit))?; m.add_class::()?; Ok(()) } #[cfg(test)] mod tests { use super::convert_to_fahrenheit; #[test] fn conversion_celsius_to_fahrenheit() { assert_eq!(convert_to_fahrenheit(25.0), 77.0); } }