diff --git a/demo/rust/src/interface.rs b/demo/rust/src/interface.rs index 9ad9313..e411a3d 100644 --- a/demo/rust/src/interface.rs +++ b/demo/rust/src/interface.rs @@ -40,7 +40,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -65,6 +65,23 @@ pub struct QModelIndex { internal_id: usize, } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct DemoQObject {} #[derive(Clone)] @@ -436,15 +453,15 @@ pub unsafe extern "C" fn fibonacci_list_free(ptr: *mut FibonacciList) { #[no_mangle] pub unsafe extern "C" fn fibonacci_list_row_count(ptr: *const FibonacciList) -> c_int { - (&*ptr).row_count() as c_int + to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_insert_rows(ptr: *mut FibonacciList, row: c_int, count: c_int) -> bool { - (&mut *ptr).insert_rows(row as usize, count as usize) + (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_remove_rows(ptr: *mut FibonacciList, row: c_int, count: c_int) -> bool { - (&mut *ptr).remove_rows(row as usize, count as usize) + (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn fibonacci_list_can_fetch_more(ptr: *const FibonacciList) -> bool { @@ -466,13 +483,13 @@ pub unsafe extern "C" fn fibonacci_list_sort( #[no_mangle] pub extern "C" fn fibonacci_list_data_fibonacci_number(ptr: *const FibonacciList, row: c_int) -> u64 { let o = unsafe { &*ptr }; - o.fibonacci_number(row as usize).into() + o.fibonacci_number(to_usize(row)).into() } #[no_mangle] pub extern "C" fn fibonacci_list_data_row(ptr: *const FibonacciList, row: c_int) -> u64 { let o = unsafe { &*ptr }; - o.row(row as usize).into() + o.row(to_usize(row)).into() } pub struct FileSystemTreeQObject {} @@ -608,7 +625,7 @@ pub extern "C" fn file_system_tree_path_get( let v = o.path(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } } @@ -632,11 +649,11 @@ pub unsafe extern "C" fn file_system_tree_row_count( item: usize, valid: bool, ) -> c_int { - if valid { - (&*ptr).row_count(Some(item)) as c_int + to_c_int(if valid { + (&*ptr).row_count(Some(item)) } else { - (&*ptr).row_count(None) as c_int - } + (&*ptr).row_count(None) + }) } #[no_mangle] pub unsafe extern "C" fn file_system_tree_can_fetch_more( @@ -674,16 +691,16 @@ pub unsafe extern "C" fn file_system_tree_index( row: c_int, ) -> usize { if !valid { - (&*ptr).index(None, row as usize) + (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), row as usize) + (&*ptr).index(Some(item), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn file_system_tree_parent(ptr: *const FileSystemTree, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { - row: (&*ptr).row(parent) as c_int, + row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { @@ -695,7 +712,7 @@ pub unsafe extern "C" fn file_system_tree_parent(ptr: *const FileSystemTree, ind } #[no_mangle] pub unsafe extern "C" fn file_system_tree_row(ptr: *const FileSystemTree, item: usize) -> c_int { - (&*ptr).row(item) as c_int + to_c_int((&*ptr).row(item)) } #[no_mangle] @@ -707,7 +724,7 @@ pub extern "C" fn file_system_tree_data_file_icon( let o = unsafe { &*ptr }; let data = o.file_icon(item); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -719,7 +736,7 @@ pub extern "C" fn file_system_tree_data_file_name( let o = unsafe { &*ptr }; let data = o.file_name(item); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -732,7 +749,7 @@ pub extern "C" fn file_system_tree_data_file_path( let data = o.file_path(item); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } } @@ -894,11 +911,11 @@ pub unsafe extern "C" fn processes_row_count( item: usize, valid: bool, ) -> c_int { - if valid { - (&*ptr).row_count(Some(item)) as c_int + to_c_int(if valid { + (&*ptr).row_count(Some(item)) } else { - (&*ptr).row_count(None) as c_int - } + (&*ptr).row_count(None) + }) } #[no_mangle] pub unsafe extern "C" fn processes_can_fetch_more( @@ -936,16 +953,16 @@ pub unsafe extern "C" fn processes_index( row: c_int, ) -> usize { if !valid { - (&*ptr).index(None, row as usize) + (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), row as usize) + (&*ptr).index(Some(item), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn processes_parent(ptr: *const Processes, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { - row: (&*ptr).row(parent) as c_int, + row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { @@ -957,7 +974,7 @@ pub unsafe extern "C" fn processes_parent(ptr: *const Processes, index: usize) - } #[no_mangle] pub unsafe extern "C" fn processes_row(ptr: *const Processes, item: usize) -> c_int { - (&*ptr).row(item) as c_int + to_c_int((&*ptr).row(item)) } #[no_mangle] @@ -969,7 +986,7 @@ pub extern "C" fn processes_data_cmd( let o = unsafe { &*ptr }; let data = o.cmd(item); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -999,7 +1016,7 @@ pub extern "C" fn processes_data_name( let o = unsafe { &*ptr }; let data = o.name(item); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -1127,15 +1144,15 @@ pub unsafe extern "C" fn time_series_free(ptr: *mut TimeSeries) { #[no_mangle] pub unsafe extern "C" fn time_series_row_count(ptr: *const TimeSeries) -> c_int { - (&*ptr).row_count() as c_int + to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn time_series_insert_rows(ptr: *mut TimeSeries, row: c_int, count: c_int) -> bool { - (&mut *ptr).insert_rows(row as usize, count as usize) + (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn time_series_remove_rows(ptr: *mut TimeSeries, row: c_int, count: c_int) -> bool { - (&mut *ptr).remove_rows(row as usize, count as usize) + (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn time_series_can_fetch_more(ptr: *const TimeSeries) -> bool { @@ -1157,7 +1174,7 @@ pub unsafe extern "C" fn time_series_sort( #[no_mangle] pub extern "C" fn time_series_data_cos(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; - o.cos(row as usize).into() + o.cos(to_usize(row)).into() } #[no_mangle] @@ -1165,13 +1182,13 @@ pub unsafe extern "C" fn time_series_set_data_cos( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { - (&mut *ptr).set_cos(row as usize, v) + (&mut *ptr).set_cos(to_usize(row), v) } #[no_mangle] pub extern "C" fn time_series_data_sin(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; - o.sin(row as usize).into() + o.sin(to_usize(row)).into() } #[no_mangle] @@ -1179,13 +1196,13 @@ pub unsafe extern "C" fn time_series_set_data_sin( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { - (&mut *ptr).set_sin(row as usize, v) + (&mut *ptr).set_sin(to_usize(row), v) } #[no_mangle] pub extern "C" fn time_series_data_time(ptr: *const TimeSeries, row: c_int) -> f32 { let o = unsafe { &*ptr }; - o.time(row as usize).into() + o.time(to_usize(row)).into() } #[no_mangle] @@ -1193,5 +1210,5 @@ pub unsafe extern "C" fn time_series_set_data_time( ptr: *mut TimeSeries, row: c_int, v: f32, ) -> bool { - (&mut *ptr).set_time(row as usize, v) + (&mut *ptr).set_time(to_usize(row), v) } diff --git a/src/rust.cpp b/src/rust.cpp index 75b233e..1d47e6e 100644 --- a/src/rust.cpp +++ b/src/rust.cpp @@ -176,7 +176,7 @@ pub extern "C" fn %1_%2(ptr: *%3 %4)").arg(lcname, lc, f.mut ? "mut" : "const", r << " let mut " << a->name << " = String::new();\n"; r << QString(" set_string_from_utf16(&mut %1, %1_str, %1_len);\n").arg(a->name); } else if (a->type.name == "QByteArray") { - r << QString(" let %1 = unsafe { slice::from_raw_parts(%1_str as *const u8, %1_len as usize) };\n").arg(a->name); + r << QString(" let %1 = unsafe { slice::from_raw_parts(%1_str as *const u8, to_usize(%1_len)) };\n").arg(a->name); } } if (f.mut) { @@ -438,7 +438,7 @@ pub extern "C" fn %2_get( let o = unsafe { &*ptr }; let v = o.%3(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } )").arg(o.name, base, snakeCase(p.name), p.type.name); if (p.write && p.type.name == "QString") { @@ -456,7 +456,7 @@ pub extern "C" fn %2_set(ptr: *mut %1, v: *const c_ushort, len: c_int) { #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; - let v = unsafe { slice::from_raw_parts(v as *const u8, len as usize) }; + let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_%3(v); } )").arg(o.name, base, snakeCase(p.name)); @@ -473,7 +473,7 @@ pub extern "C" fn %2_get( let v = o.%3(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } } )").arg(o.name, base, snakeCase(p.name), p.type.name); @@ -492,7 +492,7 @@ pub extern "C" fn %2_set(ptr: *mut %1, v: *const c_ushort, len: c_int) { #[no_mangle] pub extern "C" fn %2_set(ptr: *mut %1, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; - let v = unsafe { slice::from_raw_parts(v as *const u8, len as usize) }; + let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_%3(Some(v.into())); } )").arg(o.name, base, snakeCase(p.name)); @@ -548,15 +548,15 @@ pub extern "C" fn %2_set_none(ptr: *mut %1) { r << QString(R"( #[no_mangle] pub unsafe extern "C" fn %2_row_count(ptr: *const %1) -> c_int { - (&*ptr).row_count() as c_int + to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn %2_insert_rows(ptr: *mut %1, row: c_int, count: c_int) -> bool { - (&mut *ptr).insert_rows(row as usize, count as usize) + (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn %2_remove_rows(ptr: *mut %1, row: c_int, count: c_int) -> bool { - (&mut *ptr).remove_rows(row as usize, count as usize) + (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn %2_can_fetch_more(ptr: *const %1) -> bool { @@ -583,11 +583,11 @@ pub unsafe extern "C" fn %2_row_count( item: usize, valid: bool, ) -> c_int { - if valid { - (&*ptr).row_count(Some(item)) as c_int + to_c_int(if valid { + (&*ptr).row_count(Some(item)) } else { - (&*ptr).row_count(None) as c_int - } + (&*ptr).row_count(None) + }) } #[no_mangle] pub unsafe extern "C" fn %2_can_fetch_more( @@ -625,16 +625,16 @@ pub unsafe extern "C" fn %2_index( row: c_int, ) -> usize { if !valid { - (&*ptr).index(None, row as usize) + (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), row as usize) + (&*ptr).index(Some(item), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn %2_parent(ptr: *const %1, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { - row: (&*ptr).row(parent) as c_int, + row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { @@ -646,13 +646,13 @@ pub unsafe extern "C" fn %2_parent(ptr: *const %1, index: usize) -> QModelIndex } #[no_mangle] pub unsafe extern "C" fn %2_row(ptr: *const %1, item: usize) -> c_int { - (&*ptr).row(item) as c_int + to_c_int((&*ptr).row(item)) } )").arg(o.name, lcname); } if (o.type != ObjectType::Object) { QString indexDecl = ", row: c_int"; - QString index = "row as usize"; + QString index = "to_usize(row)"; if (o.type == ObjectType::Tree) { indexDecl = ", item: usize"; index = "item"; @@ -669,7 +669,7 @@ pub extern "C" fn %2_data_%3( let o = unsafe { &*ptr }; let data = o.%3(%5); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.type.name); } else if (ip.type.isComplex()) { @@ -684,7 +684,7 @@ pub extern "C" fn %2_data_%3( let data = o.%3(%5); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.type.name); @@ -723,7 +723,7 @@ pub extern "C" fn %2_set_data_%3( s: *const c_char, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; - let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, to_usize(len)) }; o.set_%3(%5, %6) } )").arg(o.name, lcname, snakeCase(ip.name), indexDecl, index, ip.optional ?"Some(slice)" :"slice"); @@ -819,7 +819,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -849,6 +849,31 @@ pub struct QModelIndex { row: c_int, internal_id: usize, } +)"; + } + + if (hasString || hasByteArray || hasListOrTree) { + r << R"( + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + +)"; + } + + if (hasString || hasByteArray || hasListOrTree) { + r << R"( +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + )"; } } diff --git a/templates/qt_quick/rust/src/interface.rs b/templates/qt_quick/rust/src/interface.rs index 1a8e317..50503ea 100644 --- a/templates/qt_quick/rust/src/interface.rs +++ b/templates/qt_quick/rust/src/interface.rs @@ -14,7 +14,7 @@ use implementation::*; pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -23,6 +23,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct SimpleQObject {} #[derive(Clone)] @@ -79,7 +96,7 @@ pub extern "C" fn simple_message_get( let o = unsafe { &*ptr }; let v = o.message(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] diff --git a/templates/qt_widgets/rust/src/interface.rs b/templates/qt_widgets/rust/src/interface.rs index 1a8e317..50503ea 100644 --- a/templates/qt_widgets/rust/src/interface.rs +++ b/templates/qt_widgets/rust/src/interface.rs @@ -14,7 +14,7 @@ use implementation::*; pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -23,6 +23,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct SimpleQObject {} #[derive(Clone)] @@ -79,7 +96,7 @@ pub extern "C" fn simple_message_get( let o = unsafe { &*ptr }; let v = o.message(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] diff --git a/tests/rust_functions/src/interface.rs b/tests/rust_functions/src/interface.rs index 1758eb6..a2aed64 100644 --- a/tests/rust_functions/src/interface.rs +++ b/tests/rust_functions/src/interface.rs @@ -14,7 +14,7 @@ use implementation::*; pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -23,6 +23,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct PersonQObject {} #[derive(Clone)] @@ -85,7 +102,7 @@ pub extern "C" fn person_user_name_get( let o = unsafe { &*ptr }; let v = o.user_name(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] @@ -136,8 +153,8 @@ pub extern "C" fn person_quote(ptr: *const Person, prefix_str: *const c_ushort, #[no_mangle] pub extern "C" fn person_quote_bytes(ptr: *const Person, prefix_str: *const c_char, prefix_len: c_int, suffix_str: *const c_char, suffix_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { - let prefix = unsafe { slice::from_raw_parts(prefix_str as *const u8, prefix_len as usize) }; - let suffix = unsafe { slice::from_raw_parts(suffix_str as *const u8, suffix_len as usize) }; + let prefix = unsafe { slice::from_raw_parts(prefix_str as *const u8, to_usize(prefix_len)) }; + let suffix = unsafe { slice::from_raw_parts(suffix_str as *const u8, to_usize(suffix_len)) }; let o = unsafe { &*ptr }; let r = o.quote_bytes(prefix, suffix); let s: *const c_char = r.as_ptr() as (*const c_char); diff --git a/tests/rust_list/src/interface.rs b/tests/rust_list/src/interface.rs index ba7e7a2..cd2adff 100644 --- a/tests/rust_list/src/interface.rs +++ b/tests/rust_list/src/interface.rs @@ -40,7 +40,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -62,6 +62,23 @@ pub struct QModelIndex { internal_id: usize, } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct PersonsQObject {} #[derive(Clone)] @@ -171,15 +188,15 @@ pub unsafe extern "C" fn persons_free(ptr: *mut Persons) { #[no_mangle] pub unsafe extern "C" fn persons_row_count(ptr: *const Persons) -> c_int { - (&*ptr).row_count() as c_int + to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn persons_insert_rows(ptr: *mut Persons, row: c_int, count: c_int) -> bool { - (&mut *ptr).insert_rows(row as usize, count as usize) + (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn persons_remove_rows(ptr: *mut Persons, row: c_int, count: c_int) -> bool { - (&mut *ptr).remove_rows(row as usize, count as usize) + (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn persons_can_fetch_more(ptr: *const Persons) -> bool { @@ -205,9 +222,9 @@ pub extern "C" fn persons_data_user_name( set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; - let data = o.user_name(row as usize); + let data = o.user_name(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -218,5 +235,5 @@ pub extern "C" fn persons_set_data_user_name( let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); - o.set_user_name(row as usize, v) + o.set_user_name(to_usize(row), v) } diff --git a/tests/rust_list_types/src/interface.rs b/tests/rust_list_types/src/interface.rs index d2606fe..be4f34b 100644 --- a/tests/rust_list_types/src/interface.rs +++ b/tests/rust_list_types/src/interface.rs @@ -40,7 +40,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -65,6 +65,23 @@ pub struct QModelIndex { internal_id: usize, } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct ListQObject {} #[derive(Clone)] @@ -204,15 +221,15 @@ pub unsafe extern "C" fn list_free(ptr: *mut List) { #[no_mangle] pub unsafe extern "C" fn list_row_count(ptr: *const List) -> c_int { - (&*ptr).row_count() as c_int + to_c_int((&*ptr).row_count()) } #[no_mangle] pub unsafe extern "C" fn list_insert_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { - (&mut *ptr).insert_rows(row as usize, count as usize) + (&mut *ptr).insert_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn list_remove_rows(ptr: *mut List, row: c_int, count: c_int) -> bool { - (&mut *ptr).remove_rows(row as usize, count as usize) + (&mut *ptr).remove_rows(to_usize(row), to_usize(count)) } #[no_mangle] pub unsafe extern "C" fn list_can_fetch_more(ptr: *const List) -> bool { @@ -234,7 +251,7 @@ pub unsafe extern "C" fn list_sort( #[no_mangle] pub extern "C" fn list_data_boolean(ptr: *const List, row: c_int) -> bool { let o = unsafe { &*ptr }; - o.boolean(row as usize).into() + o.boolean(to_usize(row)).into() } #[no_mangle] @@ -242,7 +259,7 @@ pub unsafe extern "C" fn list_set_data_boolean( ptr: *mut List, row: c_int, v: bool, ) -> bool { - (&mut *ptr).set_boolean(row as usize, v) + (&mut *ptr).set_boolean(to_usize(row), v) } #[no_mangle] @@ -252,9 +269,9 @@ pub extern "C" fn list_data_bytearray( set: fn(*mut QByteArray, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; - let data = o.bytearray(row as usize); + let data = o.bytearray(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -263,14 +280,14 @@ pub extern "C" fn list_set_data_bytearray( s: *const c_char, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; - let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; - o.set_bytearray(row as usize, slice) + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, to_usize(len)) }; + o.set_bytearray(to_usize(row), slice) } #[no_mangle] pub extern "C" fn list_data_f32(ptr: *const List, row: c_int) -> f32 { let o = unsafe { &*ptr }; - o.f32(row as usize).into() + o.f32(to_usize(row)).into() } #[no_mangle] @@ -278,13 +295,13 @@ pub unsafe extern "C" fn list_set_data_f32( ptr: *mut List, row: c_int, v: f32, ) -> bool { - (&mut *ptr).set_f32(row as usize, v) + (&mut *ptr).set_f32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_f64(ptr: *const List, row: c_int) -> f64 { let o = unsafe { &*ptr }; - o.f64(row as usize).into() + o.f64(to_usize(row)).into() } #[no_mangle] @@ -292,13 +309,13 @@ pub unsafe extern "C" fn list_set_data_f64( ptr: *mut List, row: c_int, v: f64, ) -> bool { - (&mut *ptr).set_f64(row as usize, v) + (&mut *ptr).set_f64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i16(ptr: *const List, row: c_int) -> i16 { let o = unsafe { &*ptr }; - o.i16(row as usize).into() + o.i16(to_usize(row)).into() } #[no_mangle] @@ -306,13 +323,13 @@ pub unsafe extern "C" fn list_set_data_i16( ptr: *mut List, row: c_int, v: i16, ) -> bool { - (&mut *ptr).set_i16(row as usize, v) + (&mut *ptr).set_i16(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i32(ptr: *const List, row: c_int) -> i32 { let o = unsafe { &*ptr }; - o.i32(row as usize).into() + o.i32(to_usize(row)).into() } #[no_mangle] @@ -320,13 +337,13 @@ pub unsafe extern "C" fn list_set_data_i32( ptr: *mut List, row: c_int, v: i32, ) -> bool { - (&mut *ptr).set_i32(row as usize, v) + (&mut *ptr).set_i32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i64(ptr: *const List, row: c_int) -> i64 { let o = unsafe { &*ptr }; - o.i64(row as usize).into() + o.i64(to_usize(row)).into() } #[no_mangle] @@ -334,13 +351,13 @@ pub unsafe extern "C" fn list_set_data_i64( ptr: *mut List, row: c_int, v: i64, ) -> bool { - (&mut *ptr).set_i64(row as usize, v) + (&mut *ptr).set_i64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_i8(ptr: *const List, row: c_int) -> i8 { let o = unsafe { &*ptr }; - o.i8(row as usize).into() + o.i8(to_usize(row)).into() } #[no_mangle] @@ -348,13 +365,13 @@ pub unsafe extern "C" fn list_set_data_i8( ptr: *mut List, row: c_int, v: i8, ) -> bool { - (&mut *ptr).set_i8(row as usize, v) + (&mut *ptr).set_i8(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_optional_boolean(ptr: *const List, row: c_int) -> COption { let o = unsafe { &*ptr }; - o.optional_boolean(row as usize).into() + o.optional_boolean(to_usize(row)).into() } #[no_mangle] @@ -362,12 +379,12 @@ pub unsafe extern "C" fn list_set_data_optional_boolean( ptr: *mut List, row: c_int, v: bool, ) -> bool { - (&mut *ptr).set_optional_boolean(row as usize, Some(v)) + (&mut *ptr).set_optional_boolean(to_usize(row), Some(v)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_boolean_none(ptr: *mut List, row: c_int) -> bool { - (&mut *ptr).set_optional_boolean(row as usize, None) + (&mut *ptr).set_optional_boolean(to_usize(row), None) } #[no_mangle] @@ -377,10 +394,10 @@ pub extern "C" fn list_data_optional_bytearray( set: fn(*mut QByteArray, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; - let data = o.optional_bytearray(row as usize); + let data = o.optional_bytearray(to_usize(row)); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } } @@ -390,13 +407,13 @@ pub extern "C" fn list_set_data_optional_bytearray( s: *const c_char, len: c_int, ) -> bool { let o = unsafe { &mut *ptr }; - let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, len as usize) }; - o.set_optional_bytearray(row as usize, Some(slice)) + let slice = unsafe { ::std::slice::from_raw_parts(s as *const u8, to_usize(len)) }; + o.set_optional_bytearray(to_usize(row), Some(slice)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_bytearray_none(ptr: *mut List, row: c_int) -> bool { - (&mut *ptr).set_optional_bytearray(row as usize, None) + (&mut *ptr).set_optional_bytearray(to_usize(row), None) } #[no_mangle] @@ -406,10 +423,10 @@ pub extern "C" fn list_data_optional_string( set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; - let data = o.optional_string(row as usize); + let data = o.optional_string(to_usize(row)); if let Some(data) = data { let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } } @@ -421,12 +438,12 @@ pub extern "C" fn list_set_data_optional_string( let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); - o.set_optional_string(row as usize, Some(v)) + o.set_optional_string(to_usize(row), Some(v)) } #[no_mangle] pub unsafe extern "C" fn list_set_data_optional_string_none(ptr: *mut List, row: c_int) -> bool { - (&mut *ptr).set_optional_string(row as usize, None) + (&mut *ptr).set_optional_string(to_usize(row), None) } #[no_mangle] @@ -436,9 +453,9 @@ pub extern "C" fn list_data_string( set: fn(*mut QString, *const c_char, len: c_int), ) { let o = unsafe { &*ptr }; - let data = o.string(row as usize); + let data = o.string(to_usize(row)); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle] @@ -449,13 +466,13 @@ pub extern "C" fn list_set_data_string( let o = unsafe { &mut *ptr }; let mut v = String::new(); set_string_from_utf16(&mut v, s, len); - o.set_string(row as usize, v) + o.set_string(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u16(ptr: *const List, row: c_int) -> u16 { let o = unsafe { &*ptr }; - o.u16(row as usize).into() + o.u16(to_usize(row)).into() } #[no_mangle] @@ -463,13 +480,13 @@ pub unsafe extern "C" fn list_set_data_u16( ptr: *mut List, row: c_int, v: u16, ) -> bool { - (&mut *ptr).set_u16(row as usize, v) + (&mut *ptr).set_u16(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u32(ptr: *const List, row: c_int) -> u32 { let o = unsafe { &*ptr }; - o.u32(row as usize).into() + o.u32(to_usize(row)).into() } #[no_mangle] @@ -477,13 +494,13 @@ pub unsafe extern "C" fn list_set_data_u32( ptr: *mut List, row: c_int, v: u32, ) -> bool { - (&mut *ptr).set_u32(row as usize, v) + (&mut *ptr).set_u32(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u64(ptr: *const List, row: c_int) -> u64 { let o = unsafe { &*ptr }; - o.u64(row as usize).into() + o.u64(to_usize(row)).into() } #[no_mangle] @@ -491,13 +508,13 @@ pub unsafe extern "C" fn list_set_data_u64( ptr: *mut List, row: c_int, v: u64, ) -> bool { - (&mut *ptr).set_u64(row as usize, v) + (&mut *ptr).set_u64(to_usize(row), v) } #[no_mangle] pub extern "C" fn list_data_u8(ptr: *const List, row: c_int) -> u8 { let o = unsafe { &*ptr }; - o.u8(row as usize).into() + o.u8(to_usize(row)).into() } #[no_mangle] @@ -505,5 +522,5 @@ pub unsafe extern "C" fn list_set_data_u8( ptr: *mut List, row: c_int, v: u8, ) -> bool { - (&mut *ptr).set_u8(row as usize, v) + (&mut *ptr).set_u8(to_usize(row), v) } diff --git a/tests/rust_object/src/interface.rs b/tests/rust_object/src/interface.rs index a23660f..0dc2d3a 100644 --- a/tests/rust_object/src/interface.rs +++ b/tests/rust_object/src/interface.rs @@ -14,7 +14,7 @@ use implementation::*; pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -23,6 +23,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct PersonQObject {} #[derive(Clone)] @@ -79,7 +96,7 @@ pub extern "C" fn person_user_name_get( let o = unsafe { &*ptr }; let v = o.user_name(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] diff --git a/tests/rust_object_types/src/interface.rs b/tests/rust_object_types/src/interface.rs index e14d231..cf3e522 100644 --- a/tests/rust_object_types/src/interface.rs +++ b/tests/rust_object_types/src/interface.rs @@ -40,7 +40,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -52,6 +52,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { pub enum QByteArray {} + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct ObjectQObject {} #[derive(Clone)] @@ -294,13 +311,13 @@ pub extern "C" fn object_bytearray_get( let o = unsafe { &*ptr }; let v = o.bytearray(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] pub extern "C" fn object_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; - let v = unsafe { slice::from_raw_parts(v as *const u8, len as usize) }; + let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_bytearray(v); } @@ -393,14 +410,14 @@ pub extern "C" fn object_optional_bytearray_get( let v = o.optional_bytearray(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } } #[no_mangle] pub extern "C" fn object_optional_bytearray_set(ptr: *mut Object, v: *const c_char, len: c_int) { let o = unsafe { &mut *ptr }; - let v = unsafe { slice::from_raw_parts(v as *const u8, len as usize) }; + let v = unsafe { slice::from_raw_parts(v as *const u8, to_usize(len)) }; o.set_optional_bytearray(Some(v.into())); } @@ -420,7 +437,7 @@ pub extern "C" fn object_optional_string_get( let v = o.optional_string(); if let Some(v) = v { let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } } @@ -466,7 +483,7 @@ pub extern "C" fn object_string_get( let o = unsafe { &*ptr }; let v = o.string(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] diff --git a/tests/rust_objects/src/interface.rs b/tests/rust_objects/src/interface.rs index 7c39126..371ca53 100644 --- a/tests/rust_objects/src/interface.rs +++ b/tests/rust_objects/src/interface.rs @@ -14,7 +14,7 @@ use implementation::*; pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -23,6 +23,23 @@ fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct GroupQObject {} #[derive(Clone)] @@ -137,7 +154,7 @@ pub extern "C" fn inner_object_description_get( let o = unsafe { &*ptr }; let v = o.description(); let s: *const c_char = v.as_ptr() as (*const c_char); - set(p, s, v.len() as c_int); + set(p, s, to_c_int(v.len())); } #[no_mangle] diff --git a/tests/rust_tree/src/interface.rs b/tests/rust_tree/src/interface.rs index 594f00a..afa7b8e 100644 --- a/tests/rust_tree/src/interface.rs +++ b/tests/rust_tree/src/interface.rs @@ -40,7 +40,7 @@ where pub enum QString {} fn set_string_from_utf16(s: &mut String, str: *const c_ushort, len: c_int) { - let utf16 = unsafe { slice::from_raw_parts(str, len as usize) }; + let utf16 = unsafe { slice::from_raw_parts(str, to_usize(len)) }; let characters = decode_utf16(utf16.iter().cloned()) .into_iter() .map(|r| r.unwrap()); @@ -62,6 +62,23 @@ pub struct QModelIndex { internal_id: usize, } + +fn to_usize(n: c_int) -> usize { + if n < 0 { + panic!("Cannot cast {} to usize", n); + } + n as usize +} + + +fn to_c_int(n: usize) -> c_int { + if n > c_int::max_value() as usize { + panic!("Cannot cast {} to c_int", n); + } + n as c_int +} + + pub struct PersonsQObject {} #[derive(Clone)] @@ -176,11 +193,11 @@ pub unsafe extern "C" fn persons_row_count( item: usize, valid: bool, ) -> c_int { - if valid { - (&*ptr).row_count(Some(item)) as c_int + to_c_int(if valid { + (&*ptr).row_count(Some(item)) } else { - (&*ptr).row_count(None) as c_int - } + (&*ptr).row_count(None) + }) } #[no_mangle] pub unsafe extern "C" fn persons_can_fetch_more( @@ -218,16 +235,16 @@ pub unsafe extern "C" fn persons_index( row: c_int, ) -> usize { if !valid { - (&*ptr).index(None, row as usize) + (&*ptr).index(None, to_usize(row)) } else { - (&*ptr).index(Some(item), row as usize) + (&*ptr).index(Some(item), to_usize(row)) } } #[no_mangle] pub unsafe extern "C" fn persons_parent(ptr: *const Persons, index: usize) -> QModelIndex { if let Some(parent) = (&*ptr).parent(index) { QModelIndex { - row: (&*ptr).row(parent) as c_int, + row: to_c_int((&*ptr).row(parent)), internal_id: parent, } } else { @@ -239,7 +256,7 @@ pub unsafe extern "C" fn persons_parent(ptr: *const Persons, index: usize) -> QM } #[no_mangle] pub unsafe extern "C" fn persons_row(ptr: *const Persons, item: usize) -> c_int { - (&*ptr).row(item) as c_int + to_c_int((&*ptr).row(item)) } #[no_mangle] @@ -251,7 +268,7 @@ pub extern "C" fn persons_data_user_name( let o = unsafe { &*ptr }; let data = o.user_name(item); let s: *const c_char = data.as_ptr() as (*const c_char); - set(d, s, data.len() as c_int); + set(d, s, to_c_int(data.len())); } #[no_mangle]