38 lines
831 B
Rust
38 lines
831 B
Rust
#![feature(specialization)]
|
|
|
|
#[macro_use]
|
|
extern crate pyo3;
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
#[pyfunction]
|
|
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
|
|
celsius * 1.8 + 32.0
|
|
}
|
|
|
|
#[pyfunction]
|
|
fn batch_convert_celsius_to_fahrenheit(celsius: Vec<f32>) -> Vec<f32> {
|
|
celsius
|
|
.iter()
|
|
.map(|temperature| convert_celsius_to_fahrenheit(temperature.clone()))
|
|
.collect()
|
|
}
|
|
|
|
#[pymodule]
|
|
fn unit_converter(_py: Python, m: &PyModule) -> PyResult<()> {
|
|
m.add_wrapped(wrap_pyfunction!(convert_celsius_to_fahrenheit))?;
|
|
m.add_wrapped(wrap_pyfunction!(batch_convert_celsius_to_fahrenheit))?;
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use super::convert_celsius_to_fahrenheit;
|
|
|
|
#[test]
|
|
fn conversion_celsius_to_fahrenheit() {
|
|
assert_eq!(convert_celsius_to_fahrenheit(25.0), 77.0);
|
|
}
|
|
}
|