diff --git a/WINGs/WINGs/WUtil.h b/WINGs/WINGs/WUtil.h index c3c5740c..5bea598d 100644 --- a/WINGs/WINGs/WUtil.h +++ b/WINGs/WINGs/WUtil.h @@ -281,7 +281,7 @@ char* wtrimspace(const char *s); */ char *wshellquote(const char *s); -/* ---[ WINGs/misc.c ]--------------------------------------------------- */ +/* ---[ wutil-rs/src/range.rs ]------------------------------------------ */ WMRange wmkrange(int start, int count); diff --git a/WINGs/misc.c b/WINGs/misc.c index 76cf1f59..b4c1e056 100644 --- a/WINGs/misc.c +++ b/WINGs/misc.c @@ -24,16 +24,6 @@ #include "error.h" -WMRange wmkrange(int start, int count) -{ - WMRange range; - - range.position = start; - range.count = count; - - return range; -} - /* * wutil_shutdown - cleanup in WUtil when user program wants to exit */ diff --git a/WINGs/wings-rs/Makefile.am b/WINGs/wings-rs/Makefile.am index 6c22a04a..da3c4f26 100644 --- a/WINGs/wings-rs/Makefile.am +++ b/WINGs/wings-rs/Makefile.am @@ -47,7 +47,7 @@ src/WINGsP.rs: ../WINGs/WINGsP.h ../../wrlib/wraster.h ../WINGs/WINGs.h ../WINGs --allowlist-type "^WM(Button|ButtonBehaviorMask)" \ --allowlist-function "^WM(CreateCustomButton|SetButtonText|SetButtonAction|SetButtonText)" \ --allowlist-function "wmkrange" \ - --allowlist-type "^WM(View|Array|DragOperationType|Point|Data|OpenPanel|SavePanel|HashTable|DraggingInfo|SelectionProcs|Rect|EventProc|Widget|Size|Color|Pixmap|FilePanel|Screen|Range|List|ListItem)" \ + --allowlist-type "^WM(View|Array|DragOperationType|Point|Data|OpenPanel|SavePanel|HashTable|DraggingInfo|SelectionProcs|Rect|EventProc|Widget|Size|Color|Pixmap|FilePanel|Screen|List|ListItem)" \ --allowlist-type "^R(Context|ContextAttributes|Image|RenderingMode|ScalingFilter|StdColormapMode|ImageFormat|Color)" \ --allowlist-type "_WINGsConfiguration" \ --allowlist-item "^WMAlignment" \ diff --git a/WINGs/wings-rs/patch_WINGsP.sh b/WINGs/wings-rs/patch_WINGsP.sh index 5475b133..00854f66 100755 --- a/WINGs/wings-rs/patch_WINGsP.sh +++ b/WINGs/wings-rs/patch_WINGsP.sh @@ -15,7 +15,7 @@ fi FILE="$1" exec sed -i -r \ - -e "1s/^/use x11::xlib::*;\nuse crate::font::ffi::WMFont;\n\n/" \ + -e "1s/^/use wutil_rs::range::ffi::*;\nuse x11::xlib::*;\nuse crate::font::ffi::WMFont;\n\n/" \ -e "s/_XftDraw/::std::ffi::c_void/g" \ -e "s/_XftFont/::std::ffi::c_void/g" \ -e "s/PangoLayout/::std::ffi::c_void/g" \ diff --git a/WINGs/wings-rs/src/font_panel.rs b/WINGs/wings-rs/src/font_panel.rs index 61c8a5ef..c94936a7 100644 --- a/WINGs/wings-rs/src/font_panel.rs +++ b/WINGs/wings-rs/src/font_panel.rs @@ -741,10 +741,10 @@ impl FontPanel { return; } WMSetTextFieldText(self.size_text.as_ptr(), (*item).text); - let Ok(len) = CStr::from_ptr((*item).text).count_bytes().try_into() else { + let Ok(len) = TryInto::::try_into(CStr::from_ptr((*item).text).count_bytes()) else { return; }; - WMSelectTextFieldRange(self.size_text.as_ptr(), wmkrange(0, len)); + WMSelectTextFieldRange(self.size_text.as_ptr(), wutil_rs::range::Range::from(0..len)); } } diff --git a/wutil-rs/Makefile.am b/wutil-rs/Makefile.am index 6b845741..f9c2844d 100644 --- a/wutil-rs/Makefile.am +++ b/wutil-rs/Makefile.am @@ -13,6 +13,7 @@ RUST_SOURCES = \ src/memory.rs \ src/notification.rs \ src/prop_list.rs \ + src/range.rs \ src/sendable.rs \ src/string.rs \ src/tree.rs diff --git a/wutil-rs/src/lib.rs b/wutil-rs/src/lib.rs index 02681092..2e7efe80 100644 --- a/wutil-rs/src/lib.rs +++ b/wutil-rs/src/lib.rs @@ -8,6 +8,7 @@ pub mod hash_table; pub mod memory; pub mod notification; pub mod prop_list; +pub mod range; pub mod sendable; pub mod string; pub mod tree; diff --git a/wutil-rs/src/range.rs b/wutil-rs/src/range.rs new file mode 100644 index 00000000..45c6a6f2 --- /dev/null +++ b/wutil-rs/src/range.rs @@ -0,0 +1,58 @@ +use std::ffi::c_int; + +#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)] +#[repr(C)] +pub struct Range { + pub start: c_int, + pub count: c_int, +} + +impl Into> for Range { + fn into(self) -> std::ops::Range { + self.start..(self.start + self.count) + } +} + +impl From> for Range { + fn from(other: std::ops::Range) -> Range { + Range { + start: other.start, + count: other.end - other.start + } + } +} + +#[cfg(test)] +mod test { + use std::ffi::c_int; + use super::Range; + + #[test] + fn from_std_range() { + assert_eq!(Range { start: 0, count: 10 }, Range::from(0..10)); + assert_eq!(Range { start: 1, count: 9 }, Range::from(1..10)); + } + + #[test] + fn into_std_range() { + assert_eq!(0..10, Into::>::into(Range { + start: 0, + count: 10 + })); + assert_eq!(1..10, Into::>::into(Range { + start: 1, + count: 9 + })); + } +} + +pub mod ffi { + use std::ffi::c_int; + + pub type WMRange = super::Range; + + #[unsafe(no_mangle)] + pub extern "C" fn wmkrange(start: c_int, count: c_int) -> WMRange { + WMRange { start, count } + } +}