Skip to content

Commit

Permalink
From -> TryFrom`
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evolvedmicrobe committed Jun 1, 2022
1 parent 080b9f7 commit 096e156
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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<LogProb> for NotNan<f64>` to `impl TryFrom<LogProb> for NotNan<f64>` to indicate the conversion can fail.
* Upgrade `strum` to version `0.24`
* Upgrade `ordered-float` to `2.0`

Expand Down
10 changes: 6 additions & 4 deletions src/stats/probs/mod.rs
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -401,9 +402,10 @@ impl From<NotNan<f64>> for LogProb {
}
}

impl From<LogProb> for NotNan<f64> {
fn from(p: LogProb) -> NotNan<f64> {
NotNan::new(*p).unwrap()
impl TryFrom<LogProb> for NotNan<f64> {
type Error = FloatIsNan;
fn try_from(p: LogProb) -> Result<Self, Self::Error> {
NotNan::new(*p)
}
}

Expand Down

0 comments on commit 096e156

Please sign in to comment.