Skip to content

Commit 687d23e

Browse files
authored
fix(libsql-sys): support non-Unix when rusqlite feature is disabled (#1971)
Currently the codepath used when rusqlite is disabled, is Unix-platform-dependent. For instance, here is the error we compiling from Windows: ``` error[E0433]: failed to resolve: could not find `unix` in `os` --> C:\Users\runneradmin\.cargo\git\checkouts\libsql-311658d335deb3b1\9b04f1d\libsql-sys\src\connection.rs:276:26 | 276 | use std::os::unix::ffi::OsStrExt; | ^^^^ could not find `unix` in `os` | note: found an item that was configured out --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14\library\std\src\os\mod.rs:27:9 note: the item is gated here --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14\library\std\src\os\mod.rs:19:1 note: found an item that was configured out --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14\library\std\src\os\mod.rs:65:9 note: the item is gated here --> /rustc/f6e511eec7342f59a25f7c0534f1dbea00d01b14\library\std\src\os\mod.rs:64:1 error[E0599]: no method named `as_bytes` found for reference `&OsStr` in the current scope --> C:\Users\runneradmin\.cargo\git\checkouts\libsql-311658d335deb3b1\9b04f1d\libsql-sys\src\connection.rs:277:73 | 277 | let path = std::ffi::CString::new(path.as_ref().as_os_str().as_bytes()) | ^^^^^^^^ | help: there is a method `as_encoded_bytes` with a similar name | 277 | let path = std::ffi::CString::new(path.as_ref().as_os_str().as_encoded_bytes()) | ~~~~~~~~~~~~~~~~ Some errors have detailed explanations: E0433, E0599. For more information about an error, try `rustc --explain E0433`. error: could not compile `libsql-sys` (lib) due to 2 previous errors ``` This patch is adding a platform-independent branch. The database path is expected to be UTF-8 by the libsql native library. However, in the real world, all Unix paths are not necessarily UTF-8. For this reason, I understand why the Unix codepath is attempting a direct conversion from OsString to CString. However, it’s not possible to do something similar on other platforms. For these platforms, we are enforcing correct UTF-8 using `to_str`. At least, it should work reasonably well in most cases.
2 parents d1c95e2 + 8ea257a commit 687d23e

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

libsql-sys/src/connection.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,27 @@ impl<W: Wal> Connection<W> {
273273

274274
#[cfg(not(feature = "rusqlite"))]
275275
let conn = unsafe {
276-
use std::os::unix::ffi::OsStrExt;
277-
let path = std::ffi::CString::new(path.as_ref().as_os_str().as_bytes())
278-
.map_err(|_| crate::error::Error::Bug("invalid database path"))?;
276+
#[cfg(unix)]
277+
let path = {
278+
use std::os::unix::ffi::OsStrExt;
279+
std::ffi::CString::new(path.as_ref().as_os_str().as_bytes()).map_err(|_| {
280+
crate::error::Error::Bug(
281+
"invalid database path containing an internal nul byte",
282+
)
283+
})?
284+
};
285+
#[cfg(not(unix))]
286+
let path = path
287+
.to_str()
288+
.ok_or_else(|| crate::error::Error::Bug("database path is not valid unicode"))
289+
.and_then(|x| {
290+
std::ffi::CString::new(x).map_err(|_| {
291+
crate::error::Error::Bug(
292+
"invalid database path containing an internal nul byte",
293+
)
294+
})
295+
})?;
296+
279297
let mut conn: *mut crate::ffi::sqlite3 = std::ptr::null_mut();
280298
// We pass a pointer to the WAL methods data to the database connection. This means
281299
// that the reference must outlive the connection. This is guaranteed by the marker in

0 commit comments

Comments
 (0)