Remove cpp 17 features and implement Rust into CPP

This commit is contained in:
Jackson Coxson
2025-08-29 14:19:28 -06:00
parent 4fde7cf06b
commit 1169408da1
41 changed files with 1638 additions and 1212 deletions

View File

@@ -6,24 +6,23 @@
namespace IdeviceFFI {
// ---------- UsbmuxdAddr ----------
std::optional<UsbmuxdAddr>
UsbmuxdAddr::tcp_new(const sockaddr* addr, socklen_t addr_len, FfiError& err) {
Result<UsbmuxdAddr, FfiError> UsbmuxdAddr::tcp_new(const sockaddr* addr, socklen_t addr_len) {
UsbmuxdAddrHandle* h = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_tcp_addr_new(addr, addr_len, &h)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_tcp_addr_new(addr, addr_len, &h));
if (e) {
return Err(e);
}
return UsbmuxdAddr(h);
return Ok(UsbmuxdAddr(h));
}
#if defined(__unix__) || defined(__APPLE__)
std::optional<UsbmuxdAddr> UsbmuxdAddr::unix_new(const std::string& path, FfiError& err) {
Result<UsbmuxdAddr, FfiError> UsbmuxdAddr::unix_new(const std::string& path) {
UsbmuxdAddrHandle* h = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_unix_addr_new(path.c_str(), &h)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_unix_addr_new(path.c_str(), &h));
if (e) {
return Err(e);
}
return UsbmuxdAddr(h);
return Ok(UsbmuxdAddr(h));
}
#endif
@@ -48,114 +47,109 @@ std::string UsbmuxdConnectionType::to_string() const {
}
// ---------- UsbmuxdDevice ----------
std::optional<std::string> UsbmuxdDevice::get_udid() const {
Option<std::string> UsbmuxdDevice::get_udid() const {
char* c = idevice_usbmuxd_device_get_udid(handle_.get());
if (!c)
return std::nullopt;
if (!c) {
return None;
}
std::string out(c);
idevice_string_free(c);
return out;
return Some(out);
}
std::optional<uint32_t> UsbmuxdDevice::get_id() const {
Option<uint32_t> UsbmuxdDevice::get_id() const {
uint32_t id = idevice_usbmuxd_device_get_device_id(handle_.get());
if (id == 0)
return std::nullopt; // adjust if 0 can be valid
return id;
if (id == 0) {
return None;
}
return Some(id);
}
std::optional<UsbmuxdConnectionType> UsbmuxdDevice::get_connection_type() const {
Option<UsbmuxdConnectionType> UsbmuxdDevice::get_connection_type() const {
uint8_t t = idevice_usbmuxd_device_get_connection_type(handle_.get());
if (t == 0)
return std::nullopt;
return UsbmuxdConnectionType(t);
if (t == 0) {
return None;
}
return Some(UsbmuxdConnectionType(t));
}
// ---------- UsbmuxdConnection ----------
std::optional<UsbmuxdConnection> UsbmuxdConnection::tcp_new(const idevice_sockaddr* addr,
idevice_socklen_t addr_len,
uint32_t tag,
FfiError& err) {
Result<UsbmuxdConnection, FfiError>
UsbmuxdConnection::tcp_new(const idevice_sockaddr* addr, idevice_socklen_t addr_len, uint32_t tag) {
UsbmuxdConnectionHandle* h = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_new_tcp_connection(addr, addr_len, tag, &h)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_new_tcp_connection(addr, addr_len, tag, &h));
if (e) {
return Err(e);
}
return UsbmuxdConnection(h);
return Ok(UsbmuxdConnection(h));
}
#if defined(__unix__) || defined(__APPLE__)
std::optional<UsbmuxdConnection>
UsbmuxdConnection::unix_new(const std::string& path, uint32_t tag, FfiError& err) {
Result<UsbmuxdConnection, FfiError> UsbmuxdConnection::unix_new(const std::string& path,
uint32_t tag) {
UsbmuxdConnectionHandle* h = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_new_unix_socket_connection(path.c_str(), tag, &h)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_new_unix_socket_connection(path.c_str(), tag, &h));
if (e) {
return Err(e);
}
return UsbmuxdConnection(h);
return Ok(UsbmuxdConnection(h));
}
#endif
std::optional<UsbmuxdConnection> UsbmuxdConnection::default_new(uint32_t tag, FfiError& err) {
Result<UsbmuxdConnection, FfiError> UsbmuxdConnection::default_new(uint32_t tag) {
UsbmuxdConnectionHandle* h = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_new_default_connection(tag, &h)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_new_default_connection(tag, &h));
if (e) {
return Err(e);
}
return UsbmuxdConnection(h);
return Ok(UsbmuxdConnection(h));
}
std::optional<std::vector<UsbmuxdDevice>> UsbmuxdConnection::get_devices(FfiError& err) const {
Result<std::vector<UsbmuxdDevice>, FfiError> UsbmuxdConnection::get_devices() const {
UsbmuxdDeviceHandle** list = nullptr;
int count = 0;
if (IdeviceFfiError* e = idevice_usbmuxd_get_devices(handle_.get(), &list, &count)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_get_devices(handle_.get(), &list, &count));
if (e) {
return Err(e);
}
std::vector<UsbmuxdDevice> out;
out.reserve(count);
for (int i = 0; i < count; ++i) {
out.emplace_back(UsbmuxdDevice::adopt(list[i]));
}
return out;
return Ok(std::move(out));
}
std::optional<std::string> UsbmuxdConnection::get_buid(FfiError& err) const {
char* c = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_get_buid(handle_.get(), &c)) {
err = FfiError(e);
return std::nullopt;
Result<std::string, FfiError> UsbmuxdConnection::get_buid() const {
char* c = nullptr;
FfiError e(idevice_usbmuxd_get_buid(handle_.get(), &c));
if (e) {
return Err(e);
}
std::string out(c);
idevice_string_free(c);
return out;
return Ok(out);
}
std::optional<PairingFile> UsbmuxdConnection::get_pair_record(const std::string& udid,
FfiError& err) {
Result<PairingFile, FfiError> UsbmuxdConnection::get_pair_record(const std::string& udid) {
IdevicePairingFile* pf = nullptr;
if (IdeviceFfiError* e = idevice_usbmuxd_get_pair_record(handle_.get(), udid.c_str(), &pf)) {
err = FfiError(e);
return std::nullopt;
FfiError e(idevice_usbmuxd_get_pair_record(handle_.get(), udid.c_str(), &pf));
if (e) {
return Err(e);
}
return PairingFile(pf);
return Ok(PairingFile(pf));
}
std::optional<Idevice> UsbmuxdConnection::connect_to_device(uint32_t device_id,
uint16_t port,
const std::string& path,
FfiError& err) && {
Result<Idevice, FfiError> UsbmuxdConnection::connect_to_device(uint32_t device_id,
uint16_t port,
const std::string& path) && {
UsbmuxdConnectionHandle* raw = handle_.release();
IdeviceHandle* out = nullptr;
IdeviceFfiError* e =
idevice_usbmuxd_connect_to_device(raw, device_id, port, path.c_str(), &out);
FfiError e(idevice_usbmuxd_connect_to_device(raw, device_id, port, path.c_str(), &out));
if (e) {
err = FfiError(e);
return std::nullopt;
return Err(e);
}
return Idevice::adopt(out);
return Ok(Idevice::adopt(out));
}
} // namespace IdeviceFFI