Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions crates/edit/src/icu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::cmp::Ordering;
use std::ffi::{CStr, c_char};
use std::mem::MaybeUninit;
use std::ops::Range;
use std::ptr::{null, null_mut};
use std::ptr::{self, null, null_mut};
use std::sync::OnceLock;
use std::{fmt, mem};

Expand Down Expand Up @@ -348,15 +348,15 @@ impl Text {

let ut = unsafe { &mut *ptr };
ut.p_funcs = &FUNCS;
ut.context = tb as *const TextBuffer as *mut _;
ut.context = ptr::from_ref(tb).cast();
ut.a = -1;

Ok(Self(ut))
}
}

fn text_buffer_from_utext<'a>(ut: &icu_ffi::UText) -> &'a TextBuffer {
unsafe { &*(ut.context as *const TextBuffer) }
unsafe { &*(ut.context.cast()) }
}

fn double_cache_from_utext<'a>(ut: &icu_ffi::UText) -> &'a mut DoubleCache {
Expand Down Expand Up @@ -1263,10 +1263,10 @@ mod icu_ffi {
pub chunk_contents: *const u16,
pub p_funcs: &'static UTextFuncs,
pub p_extra: *mut c_void,
pub context: *mut c_void,
pub p: *mut c_void,
pub q: *mut c_void,
pub r: *mut c_void,
pub context: *const c_void,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth noting that the const void* declaration in ICU is just by convention from 21 years ago. The same is actually true for p, q, and r, which are also declared as const void*. Meanwhile, even the official ICU UText adapters use p/q/r as mutable pointers.

@xtqqczze xtqqczze Aug 1, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, you’d use a crate like rust_icu rather than defining the bindings manually:

https://github.com/google/rust_icu/blob/4bc9b372dfae21f806f72486f8c33f213ef6550d/rust_icu_sys/bindgen/lib_79.rs#L1339

There, context, p, q, and r are all *const pointers, matching the C declarations, since the bindings are automatically generated by rust-bindgen.

pub p: *const c_void,
pub q: *const c_void,
pub r: *const c_void,
pub priv_p: *mut c_void,
pub a: i64,
pub b: i32,
Expand Down