rusticl/style: add util for conversion with err
NEW util/conversion.rs Adds traits `TryFromWithErr` and `TryIntoWithErr` and their implementation for types with `TryFrom` and `TryInto` traits former ``` try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY) try_from(val).map_err(|_| CL_OUT_OF_HOST_MEMORY) ``` can now be written as ``` try_into_with_err(CL_OUT_OF_HOST_MEMORY) try_from_with_err(val, CL_OUT_OF_HOST_MEMORY) ``` Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33267>
This commit is contained in:
@@ -6,6 +6,7 @@ use crate::core::memory::MemBase;
|
||||
use crate::core::program::Program;
|
||||
use crate::core::queue::Queue;
|
||||
|
||||
use mesa_rust_util::conversion::*;
|
||||
use rusticl_opencl_gen::*;
|
||||
|
||||
use std::borrow::Borrow;
|
||||
@@ -341,9 +342,9 @@ where
|
||||
let vec: Result<Vec<T>, _> = self
|
||||
.vals
|
||||
.iter()
|
||||
.map(|v| T::try_from(*v).map_err(|_| CL_OUT_OF_HOST_MEMORY))
|
||||
.map(|v| T::try_from_with_err(*v, CL_OUT_OF_HOST_MEMORY))
|
||||
.collect();
|
||||
vec?.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)
|
||||
vec?.try_into_with_err(CL_OUT_OF_HOST_MEMORY)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ use crate::impl_cl_type_trait;
|
||||
use mesa_rust::pipe::resource::*;
|
||||
use mesa_rust::pipe::screen::ResourceType;
|
||||
use mesa_rust_gen::*;
|
||||
use mesa_rust_util::conversion::*;
|
||||
use mesa_rust_util::properties::Properties;
|
||||
use mesa_rust_util::ptr::TrackedPointers;
|
||||
use rusticl_opencl_gen::*;
|
||||
@@ -56,7 +57,7 @@ impl Context {
|
||||
copy: bool,
|
||||
res_type: ResourceType,
|
||||
) -> CLResult<HashMap<&'static Device, Arc<PipeResource>>> {
|
||||
let adj_size: u32 = size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?;
|
||||
let adj_size: u32 = size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?;
|
||||
let mut res = HashMap::new();
|
||||
for &dev in &self.devs {
|
||||
let mut resource = None;
|
||||
@@ -102,22 +103,12 @@ impl Context {
|
||||
) -> CLResult<HashMap<&'static Device, Arc<PipeResource>>> {
|
||||
let pipe_format = format.to_pipe_format().unwrap();
|
||||
|
||||
let width = desc
|
||||
.image_width
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)?;
|
||||
let height = desc
|
||||
.image_height
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)?;
|
||||
let depth = desc
|
||||
.image_depth
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)?;
|
||||
let width = desc.image_width.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?;
|
||||
let height = desc.image_height.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?;
|
||||
let depth = desc.image_depth.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?;
|
||||
let array_size = desc
|
||||
.image_array_size
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)?;
|
||||
.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?;
|
||||
let target = cl_mem_type_to_texture_target(desc.image_type);
|
||||
|
||||
let mut res = HashMap::new();
|
||||
|
||||
@@ -16,6 +16,7 @@ use mesa_rust::pipe::resource::*;
|
||||
use mesa_rust::pipe::screen::ResourceType;
|
||||
use mesa_rust::pipe::transfer::*;
|
||||
use mesa_rust_gen::*;
|
||||
use mesa_rust_util::conversion::*;
|
||||
use mesa_rust_util::properties::Properties;
|
||||
use mesa_rust_util::ptr::AllocSize;
|
||||
use mesa_rust_util::ptr::TrackedPointers;
|
||||
@@ -689,8 +690,7 @@ impl CLImageDescInfo for cl_image_desc {
|
||||
|
||||
fn row_pitch(&self) -> CLResult<u32> {
|
||||
self.image_row_pitch
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)
|
||||
.try_into_with_err(CL_OUT_OF_HOST_MEMORY)
|
||||
}
|
||||
|
||||
fn slice_pitch(&self) -> usize {
|
||||
@@ -698,15 +698,11 @@ impl CLImageDescInfo for cl_image_desc {
|
||||
}
|
||||
|
||||
fn width(&self) -> CLResult<u32> {
|
||||
self.image_width
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)
|
||||
self.image_width.try_into_with_err(CL_OUT_OF_HOST_MEMORY)
|
||||
}
|
||||
|
||||
fn height(&self) -> CLResult<u32> {
|
||||
self.image_height
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)
|
||||
self.image_height.try_into_with_err(CL_OUT_OF_HOST_MEMORY)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1186,11 +1182,7 @@ impl Buffer {
|
||||
[size, 1, 1].into(),
|
||||
CL_MEM_OBJECT_BUFFER,
|
||||
)?;
|
||||
let dst_origin: [u32; 3] = [
|
||||
dst_offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
0,
|
||||
0,
|
||||
];
|
||||
let dst_origin: [u32; 3] = [dst_offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?, 0, 0];
|
||||
|
||||
ctx.resource_copy_region(src_res, dst_res, &dst_origin, &bx);
|
||||
Ok(())
|
||||
@@ -1265,8 +1257,8 @@ impl Buffer {
|
||||
ctx.clear_buffer(
|
||||
res,
|
||||
pattern,
|
||||
offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1406,8 +1398,8 @@ impl Buffer {
|
||||
|
||||
ctx.buffer_map(
|
||||
r,
|
||||
offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
rw,
|
||||
)
|
||||
.ok_or(CL_OUT_OF_RESOURCES)
|
||||
@@ -1440,9 +1432,9 @@ impl Buffer {
|
||||
|
||||
ctx.buffer_subdata(
|
||||
r,
|
||||
offset.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
offset.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
ptr,
|
||||
size.try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
size.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1946,9 +1938,7 @@ impl Image {
|
||||
res,
|
||||
&bx,
|
||||
src,
|
||||
src_row_pitch
|
||||
.try_into()
|
||||
.map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
src_row_pitch.try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
src_slice_pitch,
|
||||
);
|
||||
}
|
||||
@@ -1971,7 +1961,7 @@ impl Image {
|
||||
res.pipe_sampler_view_template_2d_buffer(self.pipe_format, &self.buffer_2d_info()?)
|
||||
} else if res.is_buffer() {
|
||||
// we need to pass in the size of the buffer, not the width.
|
||||
let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?;
|
||||
let size = self.size.try_into_with_err(CL_OUT_OF_RESOURCES)?;
|
||||
res.pipe_sampler_view_template_1d_buffer(self.pipe_format, size)
|
||||
} else {
|
||||
res.pipe_sampler_view_template()
|
||||
@@ -1992,7 +1982,7 @@ impl Image {
|
||||
&self.buffer_2d_info()?,
|
||||
))
|
||||
} else if res.is_buffer() {
|
||||
let size = self.size.try_into().map_err(|_| CL_OUT_OF_RESOURCES)?;
|
||||
let size = self.size.try_into_with_err(CL_OUT_OF_RESOURCES)?;
|
||||
Ok(res.pipe_image_view_1d_buffer(
|
||||
self.pipe_format,
|
||||
read_write,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::api::{icd::CLResult, types::CLVec};
|
||||
|
||||
use mesa_rust_gen::*;
|
||||
use mesa_rust_util::conversion::*;
|
||||
use rusticl_opencl_gen::*;
|
||||
|
||||
use super::gl::is_cube_map_face;
|
||||
@@ -61,11 +62,11 @@ pub fn create_pipe_box(
|
||||
};
|
||||
|
||||
Ok(pipe_box {
|
||||
x: base[0].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
y: base[1].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
z: base[2].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
width: region[0].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
height: region[1].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
depth: region[2].try_into().map_err(|_| CL_OUT_OF_HOST_MEMORY)?,
|
||||
x: base[0].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
y: base[1].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
z: base[2].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
width: region[0].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
height: region[1].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
depth: region[2].try_into_with_err(CL_OUT_OF_HOST_MEMORY)?,
|
||||
})
|
||||
}
|
||||
|
||||
25
src/gallium/frontends/rusticl/util/conversion.rs
Normal file
25
src/gallium/frontends/rusticl/util/conversion.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
pub trait TryFromWithErr<T, E>: Sized {
|
||||
fn try_from_with_err(value: T, error: E) -> Result<Self, E>;
|
||||
}
|
||||
|
||||
impl<T, U, E> TryFromWithErr<U, E> for T
|
||||
where
|
||||
T: TryFrom<U>,
|
||||
{
|
||||
fn try_from_with_err(value: U, error: E) -> Result<T, E> {
|
||||
T::try_from(value).map_err(|_| error)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait TryIntoWithErr<T, E>: Sized {
|
||||
fn try_into_with_err(self, error: E) -> Result<T, E>;
|
||||
}
|
||||
|
||||
impl<T, U, E> TryIntoWithErr<T, E> for U
|
||||
where
|
||||
T: TryFromWithErr<U, E>,
|
||||
{
|
||||
fn try_into_with_err(self, error: E) -> Result<T, E> {
|
||||
T::try_from_with_err(self, error)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod assert;
|
||||
pub mod bitset;
|
||||
pub mod conversion;
|
||||
pub mod feature;
|
||||
pub mod math;
|
||||
pub mod properties;
|
||||
|
||||
Reference in New Issue
Block a user