Skip to content

Commit fa615ef

Browse files
committed
Feature gate choose_multiple_weighted on std
1 parent 22dec87 commit fa615ef

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

src/seq/index.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ use alloc::collections::BTreeSet;
1717
#[cfg(feature = "std")] use std::collections::HashSet;
1818

1919
#[cfg(feature = "alloc")]
20-
use crate::distributions::{uniform::SampleUniform, Distribution, Uniform, WeightedError};
20+
use crate::distributions::{uniform::SampleUniform, Distribution, Uniform};
21+
#[cfg(feature = "std")]
22+
use crate::distributions::WeightedError;
2123
use crate::Rng;
2224

2325
#[cfg(feature = "serde1")]
@@ -270,6 +272,8 @@ where R: Rng + ?Sized {
270272
/// `O(length + amount * log length)` time otherwise.
271273
///
272274
/// Panics if `amount > length`.
275+
#[cfg(feature = "std")]
276+
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
273277
pub fn sample_weighted<R, F, X>(
274278
rng: &mut R, length: usize, weight: F, amount: usize,
275279
) -> Result<IndexVec, WeightedError>
@@ -301,6 +305,7 @@ where
301305
/// + amount * log length)` time otherwise.
302306
///
303307
/// Panics if `amount > length`.
308+
#[cfg(feature = "std")]
304309
fn sample_efraimidis_spirakis<R, F, X, N>(
305310
rng: &mut R, length: N, weight: F, amount: N,
306311
) -> Result<IndexVec, WeightedError>
@@ -375,9 +380,6 @@ where
375380

376381
#[cfg(not(feature = "nightly"))]
377382
{
378-
#[cfg(all(feature = "alloc", not(feature = "std")))]
379-
use crate::alloc::collections::BinaryHeap;
380-
#[cfg(feature = "std")]
381383
use std::collections::BinaryHeap;
382384

383385
// Partially sort the array such that the `amount` elements with the largest
@@ -619,6 +621,7 @@ mod test {
619621
assert_eq!(v1, v2);
620622
}
621623

624+
#[cfg(feature = "std")]
622625
#[test]
623626
fn test_sample_weighted() {
624627
let seed_rng = crate::test::rng;

src/seq/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ pub trait SliceRandom {
212212
/// println!("{:?}", choices.choose_multiple_weighted(&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
213213
/// ```
214214
/// [`choose_multiple`]: SliceRandom::choose_multiple
215-
#[cfg(feature = "alloc")]
215+
//
216+
// Note: this is feature-gated on std due to usage of f64::powf.
217+
// If necessary, we may use alloc+libm as an alternative (see PR #1089).
218+
#[cfg(feature = "std")]
219+
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
216220
fn choose_multiple_weighted<R, F, X>(
217221
&self, rng: &mut R, amount: usize, weight: F,
218222
) -> Result<SliceChooseIter<Self, Self::Item>, WeightedError>
@@ -556,7 +560,7 @@ impl<T> SliceRandom for [T] {
556560
Ok(&mut self[distr.sample(rng)])
557561
}
558562

559-
#[cfg(feature = "alloc")]
563+
#[cfg(feature = "std")]
560564
fn choose_multiple_weighted<R, F, X>(
561565
&self, rng: &mut R, amount: usize, weight: F,
562566
) -> Result<SliceChooseIter<Self, Self::Item>, WeightedError>
@@ -1228,7 +1232,7 @@ mod test {
12281232
}
12291233

12301234
#[test]
1231-
#[cfg(feature = "alloc")]
1235+
#[cfg(feature = "std")]
12321236
fn test_multiple_weighted_edge_cases() {
12331237
use super::*;
12341238

@@ -1308,7 +1312,7 @@ mod test {
13081312
}
13091313

13101314
#[test]
1311-
#[cfg(feature = "alloc")]
1315+
#[cfg(feature = "std")]
13121316
fn test_multiple_weighted_distributions() {
13131317
use super::*;
13141318

0 commit comments

Comments
 (0)