Skip to content

Commit b176f6d

Browse files
committed
Replace usage of deprecated PyO3 downcast
1 parent a61411f commit b176f6d

File tree

10 files changed

+44
-50
lines changed

10 files changed

+44
-50
lines changed

pysplashsurf/src/aabb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ impl PyAabb3d {
6161
let py = points.py();
6262
let element_type = points.dtype();
6363
if element_type.is_equiv_to(&np::dtype::<f32>(py)) {
64-
Self::from_points_generic(points.downcast::<PyArray2<f32>>()?)
64+
Self::from_points_generic(points.cast::<PyArray2<f32>>()?)
6565
} else if element_type.is_equiv_to(&np::dtype::<f64>(py)) {
66-
Self::from_points_generic(points.downcast::<PyArray2<f64>>()?)
66+
Self::from_points_generic(points.cast::<PyArray2<f64>>()?)
6767
} else {
6868
Err(pyerr_unsupported_scalar())
6969
}

pysplashsurf/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn pysplashsurf(m: &Bound<'_, PyModule>) -> PyResult<()> {
7070
#[pyo3(name = "run_splashsurf")]
7171
fn run_splashsurf_py<'py>(args: Bound<'py, PyList>) -> PyResult<()> {
7272
cli::run_splashsurf(args.iter().map(|arg| {
73-
arg.downcast::<PyString>()
73+
arg.cast::<PyString>()
7474
.expect("argument wasn't a string")
7575
.extract::<String>()
7676
.unwrap()

pysplashsurf/src/marching_cubes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ pub fn marching_cubes<'py>(
156156
}
157157
}
158158

159-
if let Ok(values) = values.downcast::<PyArray3<f32>>() {
159+
if let Ok(values) = values.cast::<PyArray3<f32>>() {
160160
triangulate_density_map_generic(
161161
&values,
162162
iso_surface_threshold as f32,
163163
cube_size as f32,
164164
translation.map(|t| t.map(|t| t as f32)),
165165
return_grid,
166166
)
167-
} else if let Ok(values) = values.downcast::<PyArray3<f64>>() {
167+
} else if let Ok(values) = values.cast::<PyArray3<f64>>() {
168168
triangulate_density_map_generic(
169169
&values,
170170
iso_surface_threshold,

pysplashsurf/src/mesh.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn view_triangles_generic<'py>(
2929
) -> PyResult<Bound<'py, PyArray2<NumpyUsize>>> {
3030
let vertex_indices: &[NumpyUsize] = bytemuck::cast_slice(triangles);
3131
let view = utils::view_generic(vertex_indices, &[triangles.len(), 3], container)?.into_any();
32-
Ok(view.downcast_into::<PyArray2<NumpyUsize>>()?)
32+
Ok(view.cast_into::<PyArray2<NumpyUsize>>()?)
3333
}
3434

3535
fn compute_normals_generic<'py, R: Real + Element>(
@@ -41,16 +41,15 @@ fn compute_normals_generic<'py, R: Real + Element>(
4141

4242
Ok(PyArray::from_vec(py, normals_vec)
4343
.reshape([mesh.vertices().len(), 3])?
44-
.into_any()
45-
.downcast_into::<PyUntypedArray>()
46-
.expect("downcast should not fail"))
44+
.cast_into::<PyUntypedArray>()
45+
.expect("cast should not fail"))
4746
}
4847

4948
pub fn get_triangle_mesh_generic<'py>(mesh: &Bound<'py, PyAny>) -> Option<Py<PyTriMesh3d>> {
5049
let py = mesh.py();
51-
if let Ok(mesh) = mesh.downcast::<PyTriMesh3d>() {
50+
if let Ok(mesh) = mesh.cast::<PyTriMesh3d>() {
5251
Some(mesh.as_unbound().clone_ref(py))
53-
} else if let Ok(data_mesh) = mesh.downcast::<PyMeshWithData>()
52+
} else if let Ok(data_mesh) = mesh.cast::<PyMeshWithData>()
5453
&& data_mesh.borrow().mesh_type() == MeshType::Tri3d
5554
{
5655
data_mesh.borrow().as_tri3d(py)
@@ -380,11 +379,11 @@ impl PyMeshAttribute {
380379
where
381380
PyMeshAttribute: From<OwnedMeshAttribute<R>>,
382381
{
383-
let data = if let Ok(data) = data.downcast::<PyArray1<u64>>() {
382+
let data = if let Ok(data) = data.cast::<PyArray1<u64>>() {
384383
OwnedAttributeData::ScalarU64(data.try_readonly()?.as_array().to_vec().into())
385-
} else if let Ok(data) = data.downcast::<PyArray1<R>>() {
384+
} else if let Ok(data) = data.cast::<PyArray1<R>>() {
386385
OwnedAttributeData::ScalarReal(data.try_readonly()?.as_array().to_vec().into())
387-
} else if let Ok(data) = data.downcast::<PyArray2<R>>() {
386+
} else if let Ok(data) = data.cast::<PyArray2<R>>() {
388387
let data_vec = data.try_readonly()?.as_slice()?.to_vec();
389388
if data.shape()[1] == 1 {
390389
OwnedAttributeData::ScalarReal(bytemuck::cast_vec(data_vec).into())
@@ -584,10 +583,10 @@ impl PyMeshWithData {
584583
mesh: Bound<'py, PyAny>,
585584
) -> PyResult<Self> {
586585
if mesh.is_instance_of::<PyTriMesh3d>() {
587-
let mesh = mesh.downcast_into::<PyTriMesh3d>()?;
586+
let mesh = mesh.cast_into::<PyTriMesh3d>()?;
588587
PyMeshWithData::try_from_pymesh(mesh.py(), mesh.unbind())
589588
} else if mesh.is_instance_of::<PyMixedTriQuadMesh3d>() {
590-
let mesh = mesh.downcast_into::<PyMixedTriQuadMesh3d>()?;
589+
let mesh = mesh.cast_into::<PyMixedTriQuadMesh3d>()?;
591590
PyMeshWithData::try_from_pymesh(mesh.py(), mesh.unbind())
592591
} else {
593592
Err(PyTypeError::new_err(

pysplashsurf/src/neighborhood_search.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn neighborhood_search_spatial_hashing_parallel<'py>(
7777
let element_type = particle_positions.dtype();
7878
if element_type.is_equiv_to(&np::dtype::<f32>(py)) {
7979
let particle_positions = particle_positions
80-
.downcast::<PyArray2<f32>>()?
80+
.cast::<PyArray2<f32>>()?
8181
.try_readonly()?;
8282
let particles: &[Vector3<f32>] = bytemuck::cast_slice(particle_positions.as_slice()?);
8383

@@ -89,7 +89,7 @@ pub fn neighborhood_search_spatial_hashing_parallel<'py>(
8989
);
9090
} else if element_type.is_equiv_to(&np::dtype::<f64>(py)) {
9191
let particle_positions = particle_positions
92-
.downcast::<PyArray2<f64>>()?
92+
.cast::<PyArray2<f64>>()?
9393
.try_readonly()?;
9494
let particles: &[Vector3<f64>] = bytemuck::cast_slice(particle_positions.as_slice()?);
9595

pysplashsurf/src/pipeline.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ pub fn reconstruction_pipeline<'py>(
243243
}
244244

245245
if element_type.is_equiv_to(&np::dtype::<f32>(py)) {
246-
let particles = particles.downcast::<PyArray2<f32>>()?;
246+
let particles = particles.cast::<PyArray2<f32>>()?;
247247
let reconstruction = reconstruction_pipeline_generic_impl::<IndexT, _>(
248248
particles,
249249
attributes_to_interpolate,
@@ -254,7 +254,7 @@ pub fn reconstruction_pipeline<'py>(
254254
)?;
255255
reconstruction_to_pymesh(py, reconstruction)
256256
} else if element_type.is_equiv_to(&np::dtype::<f64>(py)) {
257-
let particles = particles.downcast::<PyArray2<f64>>()?;
257+
let particles = particles.cast::<PyArray2<f64>>()?;
258258
let reconstruction = reconstruction_pipeline_generic_impl::<IndexT, _>(
259259
particles,
260260
attributes_to_interpolate,
@@ -288,21 +288,21 @@ fn reconstruction_pipeline_generic_impl<'py, I: Index, R: Real + Element>(
288288
// Collect readonly views of all attribute arrays
289289
for (key, value) in attributes_to_interpolate.iter().flatten() {
290290
let key_str: String = key
291-
.downcast::<PyString>()
291+
.cast::<PyString>()
292292
.expect("attribute key has to be a string")
293293
.extract()?;
294294

295-
if let Ok(value) = value.downcast::<PyArray1<u64>>() {
295+
if let Ok(value) = value.cast::<PyArray1<u64>>() {
296296
attr_views.push(AttributePyView::U64(value.readonly()));
297297
attr_names.push(key_str);
298-
} else if let Ok(value) = value.downcast::<PyArray1<R>>() {
298+
} else if let Ok(value) = value.cast::<PyArray1<R>>() {
299299
attr_views.push(AttributePyView::Float(value.readonly()));
300300
attr_names.push(key_str);
301-
} else if let Ok(value) = value.downcast::<PyArray2<R>>() {
301+
} else if let Ok(value) = value.cast::<PyArray2<R>>() {
302302
attr_views.push(AttributePyView::FloatVec3(value.readonly()));
303303
attr_names.push(key_str);
304304
} else {
305-
println!("Failed to downcast attribute {} to valid type", &key_str);
305+
println!("Failed to cast attribute {} to valid type", &key_str);
306306
}
307307
}
308308

pysplashsurf/src/postprocessing.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn convert_tris_to_quads<'py>(
7171
}
7272
}?;
7373

74-
if let Ok(mesh) = mesh.downcast::<PyMeshWithData>() {
74+
if let Ok(mesh) = mesh.cast::<PyMeshWithData>() {
7575
let mut data_mesh = PyMeshWithData::try_from_pymesh(py, quad_mesh)?;
7676
data_mesh.point_attributes = mesh
7777
.borrow()
@@ -124,7 +124,7 @@ pub fn laplacian_smoothing_parallel<'py>(
124124
let mut mesh = mesh.borrow_mut(py);
125125

126126
if let Some(mesh) = mesh.as_f32_mut() {
127-
let weights = weights.downcast::<PyArray1<f32>>()?.try_readonly()?;
127+
let weights = weights.cast::<PyArray1<f32>>()?.try_readonly()?;
128128
splashsurf_lib::postprocessing::par_laplacian_smoothing_inplace(
129129
mesh,
130130
&vertex_connectivity.borrow().connectivity,
@@ -133,7 +133,7 @@ pub fn laplacian_smoothing_parallel<'py>(
133133
weights.as_slice()?,
134134
);
135135
} else if let Some(mesh) = mesh.as_f64_mut() {
136-
let weights = weights.downcast::<PyArray1<f64>>()?.try_readonly()?;
136+
let weights = weights.cast::<PyArray1<f64>>()?.try_readonly()?;
137137
splashsurf_lib::postprocessing::par_laplacian_smoothing_inplace(
138138
mesh,
139139
&vertex_connectivity.borrow().connectivity,
@@ -172,15 +172,15 @@ pub fn laplacian_smoothing_normals_parallel<'py>(
172172
let py = normals.py();
173173
let element_type = normals.dtype();
174174
if element_type.is_equiv_to(&np::dtype::<f32>(py)) {
175-
let mut normals = normals.downcast::<PyArray2<f32>>()?.try_readwrite()?;
175+
let mut normals = normals.cast::<PyArray2<f32>>()?.try_readwrite()?;
176176
let normals_vec3: &mut [Vector3<f32>] = bytemuck::cast_slice_mut(normals.as_slice_mut()?);
177177
splashsurf_lib::postprocessing::par_laplacian_smoothing_normals_inplace(
178178
normals_vec3,
179179
&vertex_connectivity.borrow().connectivity,
180180
iterations,
181181
);
182182
} else if element_type.is_equiv_to(&np::dtype::<f64>(py)) {
183-
let mut normals = normals.downcast::<PyArray2<f64>>()?.try_readwrite()?;
183+
let mut normals = normals.cast::<PyArray2<f64>>()?.try_readwrite()?;
184184
let normals_vec3: &mut [Vector3<f64>] = bytemuck::cast_slice_mut(normals.as_slice_mut()?);
185185
splashsurf_lib::postprocessing::par_laplacian_smoothing_normals_inplace(
186186
normals_vec3,

pysplashsurf/src/reconstruction.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ impl PySurfaceReconstruction {
7676
let array: ArrayView1<bool> = ArrayView1::from(p.as_slice());
7777
let pyarray = unsafe { PyArray1::borrow_from_array(&array, this.into_any()) };
7878
pyarray
79-
.into_any()
80-
.downcast_into::<PyUntypedArray>()
81-
.expect("downcast should not fail")
79+
.cast_into::<PyUntypedArray>()
80+
.expect("cast should not fail")
8281
})
8382
}
8483

@@ -185,7 +184,7 @@ pub fn reconstruct_surface<'py>(
185184

186185
let element_type = particles.dtype();
187186
if element_type.is_equiv_to(&np::dtype::<f32>(py)) {
188-
let particles = particles.downcast::<PyArray2<f32>>()?.try_readonly()?;
187+
let particles = particles.cast::<PyArray2<f32>>()?.try_readonly()?;
189188
let particle_positions: &[Vector3<f32>] = bytemuck::cast_slice(particles.as_slice()?);
190189
let reconstruction = splashsurf_lib::reconstruct_surface::<IndexT, _>(
191190
particle_positions,
@@ -196,7 +195,7 @@ pub fn reconstruct_surface<'py>(
196195
.map_err(|e| anyhow!(e))?;
197196
PySurfaceReconstruction::try_from_generic(py, reconstruction)
198197
} else if element_type.is_equiv_to(&np::dtype::<f64>(py)) {
199-
let particles = particles.downcast::<PyArray2<f64>>()?.try_readonly()?;
198+
let particles = particles.cast::<PyArray2<f64>>()?.try_readonly()?;
200199
let particle_positions: &[Vector3<f64>] = bytemuck::cast_slice(particles.as_slice()?);
201200
let reconstruction =
202201
splashsurf_lib::reconstruct_surface::<IndexT, _>(particle_positions, &parameters)

pysplashsurf/src/sph_interpolation.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl PySphInterpolator {
4141
PySphInterpolator: From<SphInterpolator<R>>,
4242
{
4343
if let (Ok(particles), Ok(densities)) = (
44-
particle_positions.downcast::<PyArray2<R>>(),
45-
particle_densities.downcast::<PyArray1<R>>(),
44+
particle_positions.cast::<PyArray2<R>>(),
45+
particle_densities.cast::<PyArray1<R>>(),
4646
) {
4747
let particles = particles.try_readonly()?;
4848
let particles: &[Vector3<R>] = bytemuck::cast_slice(particles.as_slice()?);
@@ -66,17 +66,16 @@ impl PySphInterpolator {
6666
interpolation_points: &Bound<'py, PyUntypedArray>,
6767
) -> PyResult<Bound<'py, PyUntypedArray>> {
6868
let py = interpolation_points.py();
69-
if let Ok(points) = interpolation_points.downcast::<PyArray2<R>>() {
69+
if let Ok(points) = interpolation_points.cast::<PyArray2<R>>() {
7070
let points = points.try_readonly()?;
7171
let points: &[Vector3<R>] = bytemuck::cast_slice(points.as_slice()?);
7272

7373
let normals_vec = interpolator.interpolate_normals(points);
7474
Ok(bytemuck::cast_vec::<Unit<Vector3<R>>, R>(normals_vec)
7575
.into_pyarray(py)
7676
.reshape((points.len(), 3))?
77-
.into_any()
78-
.downcast_into::<PyUntypedArray>()
79-
.expect("downcast should not fail"))
77+
.cast_into::<PyUntypedArray>()
78+
.expect("cast should not fail"))
8079
} else {
8180
Err(pyerr_unsupported_scalar())
8281
}
@@ -102,17 +101,17 @@ impl PySphInterpolator {
102101
};
103102

104103
// Get the per-particle quantity as a read-only contiguous slice
105-
let quantity = if let Ok(q) = particle_quantity.downcast::<PyArray1<R>>() {
104+
let quantity = if let Ok(q) = particle_quantity.cast::<PyArray1<R>>() {
106105
q.to_dyn().try_readonly()
107-
} else if let Ok(q) = particle_quantity.downcast::<PyArray2<R>>() {
106+
} else if let Ok(q) = particle_quantity.cast::<PyArray2<R>>() {
108107
q.to_dyn().try_readonly()
109108
} else {
110109
return Err(pyerr_scalar_type_mismatch());
111110
}?;
112111
let quantity = quantity.as_slice()?;
113112

114113
let points = interpolation_points
115-
.downcast::<PyArray2<R>>()
114+
.cast::<PyArray2<R>>()
116115
.map_err(|_| pyerr_scalar_type_mismatch())?
117116
.try_readonly()?;
118117
let points: &[Vector3<R>] = bytemuck::cast_slice(points.as_slice()?);
@@ -131,9 +130,8 @@ impl PySphInterpolator {
131130
Ok(bytemuck::cast_vec::<_, R>(interpolated)
132131
.into_pyarray(py)
133132
.reshape(shape)?
134-
.into_any()
135-
.downcast_into::<PyUntypedArray>()
136-
.expect("downcast should not fail"))
133+
.cast_into::<PyUntypedArray>()
134+
.expect("cast should not fail"))
137135
}
138136

139137
let py = particle_quantity.py();

pysplashsurf/src/utils.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use ndarray::{ArrayView, IxDyn};
22
use numpy::{Element, PyArray, PyUntypedArray};
33
use pyo3::exceptions::PyTypeError;
4-
use pyo3::prelude::*;
54
use pyo3::{Bound, PyAny, PyErr, PyResult};
65
use splashsurf_lib::Real;
76
use splashsurf_lib::nalgebra::SVector;
@@ -137,8 +136,7 @@ pub(crate) fn view_generic<'py, R: Element>(
137136
ArrayView::from_shape(shape, values).map_err(anyhow::Error::new)?;
138137
let pyarray = unsafe { PyArray::borrow_from_array(&array, container) };
139138
Ok(pyarray
140-
.into_any()
141-
.downcast_into::<PyUntypedArray>()
139+
.cast_into::<PyUntypedArray>()
142140
.expect("downcast should not fail"))
143141
}
144142

0 commit comments

Comments
 (0)