From 096e156858f9abf0f2314b77cd95ca54d65d0645 Mon Sep 17 00:00:00 2001 From: Nigel Delaney Date: Tue, 31 May 2022 11:56:27 -0700 Subject: [PATCH] `From -> `TryFrom` The conversion from LogProb is not guaranteed to succeed, so we use `TryFrom` instead of `From`, as per the Rust docs: ```Note: The From trait must not fail. The From trait is intended for perfect conversions. If the conversion can fail or is not perfect, use TryFrom.``` Note this does change the API. --- CHANGELOG.md | 1 + src/stats/probs/mod.rs | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1a65587a..a5d8ffdbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [0.42.0](https://www.github.com/rust-bio/rust-bio/compare/v0.42.0...v0.41.0) (2022-05-03) +* Change `impl From for NotNan` to `impl TryFrom for NotNan` to indicate the conversion can fail. * Upgrade `strum` to version `0.24` * Upgrade `ordered-float` to `2.0` diff --git a/src/stats/probs/mod.rs b/src/stats/probs/mod.rs index 8b08161b4..c889b010f 100644 --- a/src/stats/probs/mod.rs +++ b/src/stats/probs/mod.rs @@ -10,6 +10,7 @@ pub mod adaptive_integration; pub mod cdf; pub mod errors; +use std::convert::TryFrom; use std::f64; use std::iter; use std::mem; @@ -18,7 +19,7 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign}; use itertools::Itertools; use itertools_num::linspace; use num_traits::{Float, Zero}; -use ordered_float::NotNan; +use ordered_float::{FloatIsNan, NotNan}; use crate::utils::FastExp; @@ -401,9 +402,10 @@ impl From> for LogProb { } } -impl From for NotNan { - fn from(p: LogProb) -> NotNan { - NotNan::new(*p).unwrap() +impl TryFrom for NotNan { + type Error = FloatIsNan; + fn try_from(p: LogProb) -> Result { + NotNan::new(*p) } }