Skip to content

Commit 6c1a89b

Browse files
committed
fmt
1 parent eed5b19 commit 6c1a89b

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"rust-analyzer.cargo.features": "all"
2+
"[rust]": {
3+
"editor.defaultFormatter": "rust-lang.rust-analyzer",
4+
"editor.formatOnSave": true,
5+
},
6+
"rust-analyzer.cargo.features": "all",
37
}

src/npyffi/random.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::ffi::c_void;
22

3-
43
#[repr(C)]
54
#[derive(Debug, Clone, Copy)] // TODO: can it be Clone and/or Copy?
65
pub struct npy_bitgen {
76
pub state: *mut c_void,
87
pub next_uint64: unsafe extern "C" fn(*mut c_void) -> super::npy_uint64, //nogil
98
pub next_uint32: unsafe extern "C" fn(*mut c_void) -> super::npy_uint32, //nogil
10-
pub next_double: unsafe extern "C" fn(*mut c_void) -> libc::c_double, //nogil
11-
pub next_raw: unsafe extern "C" fn(*mut c_void) -> super::npy_uint64, //nogil
9+
pub next_double: unsafe extern "C" fn(*mut c_void) -> libc::c_double, //nogil
10+
pub next_raw: unsafe extern "C" fn(*mut c_void) -> super::npy_uint64, //nogil
1211
}

src/random.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//! Safe interface for NumPy's random [`BitGenerator`]
22
3-
use pyo3::{ffi, prelude::*, sync::GILOnceCell, types::{PyCapsule, PyType}, PyTypeInfo, exceptions::PyRuntimeError};
3+
use pyo3::{
4+
exceptions::PyRuntimeError,
5+
ffi,
6+
prelude::*,
7+
sync::GILOnceCell,
8+
types::{PyCapsule, PyType},
9+
PyTypeInfo,
10+
};
411

512
use crate::npyffi::npy_bitgen;
613

@@ -39,11 +46,15 @@ pub trait BitGeneratorMethods<'py> {
3946

4047
impl<'py> BitGeneratorMethods<'py> for Bound<'py, BitGenerator> {
4148
fn bit_gen(&self) -> PyResult<BitGen<'py>> {
42-
let capsule = self.as_any().getattr("capsule")?.downcast_into::<PyCapsule>()?;
49+
let capsule = self
50+
.as_any()
51+
.getattr("capsule")?
52+
.downcast_into::<PyCapsule>()?;
4353
assert_eq!(capsule.name()?, Some(c"BitGenerator"));
4454
let ptr = capsule.pointer() as *mut npy_bitgen;
4555
// SAFETY: the lifetime of `ptr` is derived from the lifetime of `self`
46-
let ref_ = unsafe { ptr.as_mut::<'py>() }.ok_or_else(|| PyRuntimeError::new_err("Invalid BitGenerator capsule"))?;
56+
let ref_ = unsafe { ptr.as_mut::<'py>() }
57+
.ok_or_else(|| PyRuntimeError::new_err("Invalid BitGenerator capsule"))?;
4758
Ok(BitGen(ref_))
4859
}
4960
}
@@ -93,13 +104,16 @@ impl rand::RngCore for BitGen<'_> {
93104
#[cfg(test)]
94105
mod tests {
95106
use super::*;
96-
107+
97108
fn get_bit_generator<'py>(py: Python<'py>) -> PyResult<Bound<'py, BitGenerator>> {
98109
let default_rng = py.import("numpy.random")?.getattr("default_rng")?;
99-
let bit_generator = default_rng.call0()?.getattr("bit_generator")?.downcast_into::<BitGenerator>()?;
110+
let bit_generator = default_rng
111+
.call0()?
112+
.getattr("bit_generator")?
113+
.downcast_into::<BitGenerator>()?;
100114
Ok(bit_generator)
101115
}
102-
116+
103117
#[test]
104118
fn bitgen() -> PyResult<()> {
105119
Python::with_gil(|py| {
@@ -113,7 +127,7 @@ mod tests {
113127
#[test]
114128
fn rand() -> PyResult<()> {
115129
use rand::Rng as _;
116-
130+
117131
Python::with_gil(|py| {
118132
let mut bitgen = get_bit_generator(py)?.bit_gen()?;
119133
let _ = bitgen.random_ratio(2, 3);

0 commit comments

Comments
 (0)