Skip to content

Commit 7760867

Browse files
committed
Migration to Edition 2024
1 parent d8beccf commit 7760867

File tree

22 files changed

+564
-433
lines changed

22 files changed

+564
-433
lines changed

native/src/base/build.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::gen::gen_cxx_binding;
1+
use crate::codegen::gen_cxx_binding;
22

3-
#[path = "../include/gen.rs"]
4-
mod gen;
3+
#[path = "../include/codegen.rs"]
4+
mod codegen;
55

66
fn main() {
77
gen_cxx_binding("base-rs");

native/src/base/cstr.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub mod cstr_buf {
6262

6363
#[inline(always)]
6464
pub unsafe fn wrap_ptr<'a>(buf: *mut u8, len: usize) -> Utf8CStrBufRef<'a> {
65-
Utf8CStrBufRef::from_ptr(buf, len)
65+
unsafe { Utf8CStrBufRef::from_ptr(buf, len) }
6666
}
6767
}
6868

@@ -232,7 +232,9 @@ impl Utf8CStrBuf for Utf8CString {
232232
}
233233

234234
unsafe fn set_len(&mut self, len: usize) {
235-
self.0.as_mut_vec().set_len(len);
235+
unsafe {
236+
self.0.as_mut_vec().set_len(len);
237+
}
236238
}
237239

238240
fn push_str(&mut self, s: &str) -> usize {
@@ -277,7 +279,7 @@ pub struct Utf8CStrBufRef<'a> {
277279

278280
impl<'a> Utf8CStrBufRef<'a> {
279281
pub unsafe fn from_ptr(buf: *mut u8, len: usize) -> Utf8CStrBufRef<'a> {
280-
Self::from(slice_from_ptr_mut(buf, len))
282+
unsafe { Self::from(slice_from_ptr_mut(buf, len)) }
281283
}
282284
}
283285

@@ -366,12 +368,12 @@ impl Utf8CStr {
366368

367369
#[inline(always)]
368370
pub const unsafe fn from_bytes_unchecked(buf: &[u8]) -> &Utf8CStr {
369-
mem::transmute(buf)
371+
unsafe { mem::transmute(buf) }
370372
}
371373

372374
#[inline(always)]
373375
unsafe fn from_bytes_unchecked_mut(buf: &mut [u8]) -> &mut Utf8CStr {
374-
mem::transmute(buf)
376+
unsafe { mem::transmute(buf) }
375377
}
376378

377379
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> Result<&'a Utf8CStr, StrErr> {
@@ -382,8 +384,10 @@ impl Utf8CStr {
382384
}
383385

384386
pub unsafe fn from_ptr_unchecked<'a>(ptr: *const c_char) -> &'a Utf8CStr {
385-
let cstr = CStr::from_ptr(ptr);
386-
Self::from_bytes_unchecked(cstr.to_bytes_with_nul())
387+
unsafe {
388+
let cstr = CStr::from_ptr(ptr);
389+
Self::from_bytes_unchecked(cstr.to_bytes_with_nul())
390+
}
387391
}
388392

389393
#[inline(always)]

native/src/base/cxx_extern.rs

Lines changed: 101 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,42 @@ pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize {
2020
.map_or(-1_isize, |_| buf.len() as isize)
2121
}
2222

23-
#[no_mangle]
23+
#[unsafe(no_mangle)]
2424
unsafe extern "C" fn canonical_path(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
25-
match Utf8CStr::from_ptr(path) {
26-
Ok(p) => {
27-
let mut buf = cstr_buf::wrap_ptr(buf, bufsz);
28-
FsPath::from(p)
29-
.realpath(&mut buf)
30-
.map_or(-1, |_| buf.len() as isize)
25+
unsafe {
26+
match Utf8CStr::from_ptr(path) {
27+
Ok(p) => {
28+
let mut buf = cstr_buf::wrap_ptr(buf, bufsz);
29+
FsPath::from(p)
30+
.realpath(&mut buf)
31+
.map_or(-1, |_| buf.len() as isize)
32+
}
33+
Err(_) => -1,
3134
}
32-
Err(_) => -1,
3335
}
3436
}
3537

36-
#[export_name = "mkdirs"]
38+
#[unsafe(export_name = "mkdirs")]
3739
unsafe extern "C" fn mkdirs_for_cxx(path: *const c_char, mode: mode_t) -> i32 {
38-
match Utf8CStr::from_ptr(path) {
39-
Ok(p) => FsPath::from(p).mkdirs(mode).map_or(-1, |_| 0),
40-
Err(_) => -1,
40+
unsafe {
41+
match Utf8CStr::from_ptr(path) {
42+
Ok(p) => FsPath::from(p).mkdirs(mode).map_or(-1, |_| 0),
43+
Err(_) => -1,
44+
}
4145
}
4246
}
4347

44-
#[export_name = "rm_rf"]
48+
#[unsafe(export_name = "rm_rf")]
4549
unsafe extern "C" fn rm_rf_for_cxx(path: *const c_char) -> bool {
46-
match Utf8CStr::from_ptr(path) {
47-
Ok(p) => FsPath::from(p).remove_all().is_ok(),
48-
Err(_) => false,
50+
unsafe {
51+
match Utf8CStr::from_ptr(path) {
52+
Ok(p) => FsPath::from(p).remove_all().is_ok(),
53+
Err(_) => false,
54+
}
4955
}
5056
}
5157

52-
#[no_mangle]
58+
#[unsafe(no_mangle)]
5359
unsafe extern "C" fn frm_rf(fd: OwnedFd) -> bool {
5460
fn inner(fd: OwnedFd) -> io::Result<()> {
5561
Directory::try_from(fd)?.remove_all()
@@ -83,110 +89,122 @@ pub(crate) unsafe fn readlinkat_for_cxx(
8389
buf: *mut u8,
8490
bufsz: usize,
8591
) -> isize {
86-
// readlinkat() may fail on x86 platform, returning random value
87-
// instead of number of bytes placed in buf (length of link)
88-
cfg_if! {
89-
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
90-
libc::memset(buf.cast(), 0, bufsz);
91-
let mut r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
92-
if r > 0 {
93-
r = libc::strlen(buf.cast()) as isize;
94-
}
95-
} else {
96-
let r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
97-
if r >= 0 {
98-
*buf.offset(r) = b'\0';
92+
unsafe {
93+
// readlinkat() may fail on x86 platform, returning random value
94+
// instead of number of bytes placed in buf (length of link)
95+
cfg_if! {
96+
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
97+
libc::memset(buf.cast(), 0, bufsz);
98+
let mut r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
99+
if r > 0 {
100+
r = libc::strlen(buf.cast()) as isize;
101+
}
102+
} else {
103+
let r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
104+
if r >= 0 {
105+
*buf.offset(r) = b'\0';
106+
}
99107
}
100108
}
109+
r
101110
}
102-
r
103111
}
104112

105-
#[export_name = "cp_afc"]
113+
#[unsafe(export_name = "cp_afc")]
106114
unsafe extern "C" fn cp_afc_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
107-
if let Ok(src) = Utf8CStr::from_ptr(src) {
108-
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
109-
let src = FsPath::from(src);
110-
let dest = FsPath::from(dest);
111-
return src
112-
.copy_to(dest)
113-
.log_cxx_with_msg(|w| {
114-
w.write_fmt(format_args!("cp_afc {} -> {} failed", src, dest))
115-
})
116-
.is_ok();
115+
unsafe {
116+
if let Ok(src) = Utf8CStr::from_ptr(src) {
117+
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
118+
let src = FsPath::from(src);
119+
let dest = FsPath::from(dest);
120+
return src
121+
.copy_to(dest)
122+
.log_cxx_with_msg(|w| {
123+
w.write_fmt(format_args!("cp_afc {} -> {} failed", src, dest))
124+
})
125+
.is_ok();
126+
}
117127
}
128+
false
118129
}
119-
false
120130
}
121131

122-
#[export_name = "mv_path"]
132+
#[unsafe(export_name = "mv_path")]
123133
unsafe extern "C" fn mv_path_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
124-
if let Ok(src) = Utf8CStr::from_ptr(src) {
125-
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
126-
let src = FsPath::from(src);
127-
let dest = FsPath::from(dest);
128-
return src
129-
.move_to(dest)
130-
.log_cxx_with_msg(|w| {
131-
w.write_fmt(format_args!("mv_path {} -> {} failed", src, dest))
132-
})
133-
.is_ok();
134+
unsafe {
135+
if let Ok(src) = Utf8CStr::from_ptr(src) {
136+
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
137+
let src = FsPath::from(src);
138+
let dest = FsPath::from(dest);
139+
return src
140+
.move_to(dest)
141+
.log_cxx_with_msg(|w| {
142+
w.write_fmt(format_args!("mv_path {} -> {} failed", src, dest))
143+
})
144+
.is_ok();
145+
}
134146
}
147+
false
135148
}
136-
false
137149
}
138150

139-
#[export_name = "link_path"]
151+
#[unsafe(export_name = "link_path")]
140152
unsafe extern "C" fn link_path_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
141-
if let Ok(src) = Utf8CStr::from_ptr(src) {
142-
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
143-
let src = FsPath::from(src);
144-
let dest = FsPath::from(dest);
145-
return src
146-
.link_to(dest)
147-
.log_cxx_with_msg(|w| {
148-
w.write_fmt(format_args!("link_path {} -> {} failed", src, dest))
149-
})
150-
.is_ok();
153+
unsafe {
154+
if let Ok(src) = Utf8CStr::from_ptr(src) {
155+
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
156+
let src = FsPath::from(src);
157+
let dest = FsPath::from(dest);
158+
return src
159+
.link_to(dest)
160+
.log_cxx_with_msg(|w| {
161+
w.write_fmt(format_args!("link_path {} -> {} failed", src, dest))
162+
})
163+
.is_ok();
164+
}
151165
}
166+
false
152167
}
153-
false
154168
}
155169

156-
#[export_name = "clone_attr"]
170+
#[unsafe(export_name = "clone_attr")]
157171
unsafe extern "C" fn clone_attr_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
158-
if let Ok(src) = Utf8CStr::from_ptr(src) {
159-
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
160-
let src = FsPath::from(src);
161-
let dest = FsPath::from(dest);
162-
return clone_attr(src, dest)
163-
.log_cxx_with_msg(|w| {
164-
w.write_fmt(format_args!("clone_attr {} -> {} failed", src, dest))
165-
})
166-
.is_ok();
172+
unsafe {
173+
if let Ok(src) = Utf8CStr::from_ptr(src) {
174+
if let Ok(dest) = Utf8CStr::from_ptr(dest) {
175+
let src = FsPath::from(src);
176+
let dest = FsPath::from(dest);
177+
return clone_attr(src, dest)
178+
.log_cxx_with_msg(|w| {
179+
w.write_fmt(format_args!("clone_attr {} -> {} failed", src, dest))
180+
})
181+
.is_ok();
182+
}
167183
}
184+
false
168185
}
169-
false
170186
}
171187

172-
#[export_name = "fclone_attr"]
188+
#[unsafe(export_name = "fclone_attr")]
173189
unsafe extern "C" fn fclone_attr_for_cxx(a: RawFd, b: RawFd) -> bool {
174190
fclone_attr(a, b)
175191
.log_cxx_with_msg(|w| w.write_str("fclone_attr failed"))
176192
.is_ok()
177193
}
178194

179-
#[export_name = "cxx$utf8str$new"]
195+
#[unsafe(export_name = "cxx$utf8str$new")]
180196
unsafe extern "C" fn str_new(this: &mut &Utf8CStr, s: *const u8, len: usize) {
181-
*this = Utf8CStr::from_bytes(slice_from_ptr(s, len)).unwrap_or(cstr!(""));
197+
unsafe {
198+
*this = Utf8CStr::from_bytes(slice_from_ptr(s, len)).unwrap_or(cstr!(""));
199+
}
182200
}
183201

184-
#[export_name = "cxx$utf8str$ptr"]
202+
#[unsafe(export_name = "cxx$utf8str$ptr")]
185203
unsafe extern "C" fn str_ptr(this: &&Utf8CStr) -> *const u8 {
186204
this.as_ptr().cast()
187205
}
188206

189-
#[export_name = "cxx$utf8str$len"]
207+
#[unsafe(export_name = "cxx$utf8str$len")]
190208
unsafe extern "C" fn str_len(this: &&Utf8CStr) -> usize {
191209
this.len()
192210
}

native/src/base/files.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl DirEntry<'_> {
263263
}
264264

265265
unsafe fn open_fd(&self, flags: i32) -> io::Result<RawFd> {
266-
self.dir.open_raw_fd(self.name(), flags, 0)
266+
unsafe { self.dir.open_raw_fd(self.name(), flags, 0) }
267267
}
268268

269269
pub fn open_as_dir(&self) -> io::Result<Directory> {
@@ -361,7 +361,9 @@ impl Directory {
361361
}
362362

363363
unsafe fn open_raw_fd(&self, name: &CStr, flags: i32, mode: i32) -> io::Result<RawFd> {
364-
libc::openat(self.as_raw_fd(), name.as_ptr(), flags | O_CLOEXEC, mode).check_os_err()
364+
unsafe {
365+
libc::openat(self.as_raw_fd(), name.as_ptr(), flags | O_CLOEXEC, mode).check_os_err()
366+
}
365367
}
366368

367369
pub fn open_fd(&self, name: &Utf8CStr, flags: i32, mode: i32) -> io::Result<OwnedFd> {
@@ -929,7 +931,7 @@ impl Drop for MappedFile {
929931
}
930932
}
931933

932-
extern "C" {
934+
unsafe extern "C" {
933935
// Don't use the declaration from the libc crate as request should be u32 not i32
934936
fn ioctl(fd: RawFd, request: u32, ...) -> i32;
935937
}

native/src/base/misc.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ pub fn errno() -> &'static mut i32 {
1616
// When len is 0, don't care whether buf is null or not
1717
#[inline]
1818
pub unsafe fn slice_from_ptr<'a, T>(buf: *const T, len: usize) -> &'a [T] {
19-
if len == 0 {
20-
&[]
21-
} else {
22-
slice::from_raw_parts(buf, len)
19+
unsafe {
20+
if len == 0 {
21+
&[]
22+
} else {
23+
slice::from_raw_parts(buf, len)
24+
}
2325
}
2426
}
2527

2628
// When len is 0, don't care whether buf is null or not
2729
#[inline]
2830
pub unsafe fn slice_from_ptr_mut<'a, T>(buf: *mut T, len: usize) -> &'a mut [T] {
29-
if len == 0 {
30-
&mut []
31-
} else {
32-
slice::from_raw_parts_mut(buf, len)
31+
unsafe {
32+
if len == 0 {
33+
&mut []
34+
} else {
35+
slice::from_raw_parts_mut(buf, len)
36+
}
3337
}
3438
}
3539

0 commit comments

Comments
 (0)