Skip to content

Commit 4b3e9e2

Browse files
theomonnomMaxHeimbrock
authored andcommitted
Fix iOS, Android, and Windows compilation errors
1 parent 867e458 commit 4b3e9e2

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

device-info/src/native/android.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,11 @@ fn get_build_field(env: &mut jni::JNIEnv, field: &str) -> Result<String, DeviceI
5555

5656
let value = env
5757
.get_static_field(build_class, field, "Ljava/lang/String;")
58-
.map_err(|e| DeviceInfoError::Jni(format!("get Build.{field}: {e}")))?;
59-
60-
let jstr = match value {
61-
JValue::Object(obj) => obj,
62-
_ => return Err(DeviceInfoError::Jni(format!("Build.{field} is not a String"))),
63-
};
58+
.map_err(|e| DeviceInfoError::Jni(format!("get Build.{field}: {e}")))?
59+
.l()
60+
.map_err(|e| DeviceInfoError::Jni(format!("Build.{field} is not an Object: {e}")))?;
6461

65-
let jstring = jni::objects::JString::from(jstr);
62+
let jstring: jni::objects::JString = value.into();
6663
let rust_str = env
6764
.get_string(&jstring)
6865
.map_err(|e| DeviceInfoError::Jni(format!("get string Build.{field}: {e}")))?;

device-info/src/native/apple_mobile.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ fn utsname_machine() -> Result<String, DeviceInfoError> {
3535
}
3636

3737
fn ui_device_name() -> Result<String, DeviceInfoError> {
38-
use objc2::rc::Retained;
38+
use objc2::MainThreadMarker;
3939
use objc2_ui_kit::UIDevice;
4040

41-
let device = UIDevice::currentDevice();
42-
let name: Retained<objc2::runtime::NSString> = unsafe { device.name() };
41+
// UIDevice can be accessed from any thread, but currentDevice() requires
42+
// a MainThreadMarker in objc2. We assume the caller is on the main thread
43+
// or that UIDevice is safe to query (which it is in practice).
44+
let mtm = unsafe { MainThreadMarker::new_unchecked() };
45+
let device = UIDevice::currentDevice(mtm);
46+
let name = device.name();
4347
Ok(name.to_string())
4448
}
4549

device-info/src/native/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn registry_model() -> Result<String, DeviceInfoError> {
3434
let value_name: Vec<u16> = "SystemProductName\0".encode_utf16().collect();
3535

3636
unsafe {
37-
let mut hkey = 0isize;
37+
let mut hkey = std::ptr::null_mut();
3838
let status = RegOpenKeyExW(HKEY_LOCAL_MACHINE, subkey.as_ptr(), 0, KEY_READ, &mut hkey);
3939
if status != 0 {
4040
return Err(DeviceInfoError::Query("failed to open BIOS registry key".into()));

0 commit comments

Comments
 (0)