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,114 +6,115 @@ namespace IdeviceFFI {
// ---- Factories ----
std::optional<CoreDeviceProxy> CoreDeviceProxy::connect(Provider& provider, FfiError& err) {
CoreDeviceProxyHandle* out = nullptr;
if (IdeviceFfiError* e = ::core_device_proxy_connect(provider.raw(), &out)) {
err = FfiError(e);
return std::nullopt;
}
return CoreDeviceProxy::adopt(out);
Result<CoreDeviceProxy, FfiError> CoreDeviceProxy::connect(Provider &provider) {
CoreDeviceProxyHandle *out = nullptr;
FfiError e(::core_device_proxy_connect(provider.raw(), &out));
if (e) {
return Err(e);
}
return Ok(CoreDeviceProxy::adopt(out));
}
std::optional<CoreDeviceProxy> CoreDeviceProxy::from_socket(Idevice&& socket, FfiError& err) {
CoreDeviceProxyHandle* out = nullptr;
Result<CoreDeviceProxy, FfiError>
CoreDeviceProxy::from_socket(Idevice &&socket) {
CoreDeviceProxyHandle *out = nullptr;
// Rust consumes the socket regardless of result → release BEFORE call
IdeviceHandle* raw = socket.release();
// Rust consumes the socket regardless of result → release BEFORE call
IdeviceHandle *raw = socket.release();
if (IdeviceFfiError* e = ::core_device_proxy_new(raw, &out)) {
// socket is already consumed on error; do NOT touch it
err = FfiError(e);
return std::nullopt;
}
return CoreDeviceProxy::adopt(out);
FfiError e(::core_device_proxy_new(raw, &out));
if (e) {
return Err(e);
}
return Ok(CoreDeviceProxy::adopt(out));
}
// ---- IO ----
bool CoreDeviceProxy::send(const uint8_t* data, size_t len, FfiError& err) {
if (IdeviceFfiError* e = ::core_device_proxy_send(handle_.get(), data, len)) {
err = FfiError(e);
return false;
}
return true;
Result<void, FfiError> CoreDeviceProxy::send(const uint8_t *data, size_t len) {
FfiError e(::core_device_proxy_send(handle_.get(), data, len));
if (e) {
return Err(e);
}
return Ok();
}
bool CoreDeviceProxy::recv(std::vector<uint8_t>& out, FfiError& err) {
if (out.empty())
out.resize(4096); // a reasonable default; caller can pre-size
size_t actual = 0;
if (IdeviceFfiError* e =
::core_device_proxy_recv(handle_.get(), out.data(), &actual, out.size())) {
err = FfiError(e);
return false;
}
out.resize(actual);
return true;
Result<void, FfiError> CoreDeviceProxy::recv(std::vector<uint8_t> &out) {
if (out.empty())
out.resize(4096); // a reasonable default; caller can pre-size
size_t actual = 0;
FfiError e(
::core_device_proxy_recv(handle_.get(), out.data(), &actual, out.size()));
if (e) {
return Err(e);
}
out.resize(actual);
return Ok();
}
// ---- Handshake ----
std::optional<CoreClientParams> CoreDeviceProxy::get_client_parameters(FfiError& err) const {
uint16_t mtu = 0;
char* addr_c = nullptr;
char* mask_c = nullptr;
Result<CoreClientParams, FfiError>
CoreDeviceProxy::get_client_parameters() const {
uint16_t mtu = 0;
char *addr_c = nullptr;
char *mask_c = nullptr;
if (IdeviceFfiError* e =
::core_device_proxy_get_client_parameters(handle_.get(), &mtu, &addr_c, &mask_c)) {
err = FfiError(e);
return std::nullopt;
}
FfiError e(::core_device_proxy_get_client_parameters(handle_.get(), &mtu,
&addr_c, &mask_c));
if (e) {
return Err(e);
}
CoreClientParams params;
params.mtu = mtu;
if (addr_c) {
params.address = addr_c;
::idevice_string_free(addr_c);
}
if (mask_c) {
params.netmask = mask_c;
::idevice_string_free(mask_c);
}
return params;
CoreClientParams params;
params.mtu = mtu;
if (addr_c) {
params.address = addr_c;
::idevice_string_free(addr_c);
}
if (mask_c) {
params.netmask = mask_c;
::idevice_string_free(mask_c);
}
return Ok(std::move(params));
}
std::optional<std::string> CoreDeviceProxy::get_server_address(FfiError& err) const {
char* addr_c = nullptr;
if (IdeviceFfiError* e = ::core_device_proxy_get_server_address(handle_.get(), &addr_c)) {
err = FfiError(e);
return std::nullopt;
}
std::string s;
if (addr_c) {
s = addr_c;
::idevice_string_free(addr_c);
}
return s;
Result<std::string, FfiError> CoreDeviceProxy::get_server_address() const {
char *addr_c = nullptr;
FfiError e(::core_device_proxy_get_server_address(handle_.get(), &addr_c));
if (e) {
return Err(e);
}
std::string s;
if (addr_c) {
s = addr_c;
::idevice_string_free(addr_c);
}
return Ok(s);
}
std::optional<uint16_t> CoreDeviceProxy::get_server_rsd_port(FfiError& err) const {
uint16_t port = 0;
if (IdeviceFfiError* e = ::core_device_proxy_get_server_rsd_port(handle_.get(), &port)) {
err = FfiError(e);
return std::nullopt;
}
return port;
Result<uint16_t, FfiError> CoreDeviceProxy::get_server_rsd_port() const {
uint16_t port = 0;
FfiError e(::core_device_proxy_get_server_rsd_port(handle_.get(), &port));
if (e) {
return Err(e);
}
return Ok(port);
}
// ---- Adapter creation (consumes *this) ----
std::optional<Adapter> CoreDeviceProxy::create_tcp_adapter(FfiError& err) && {
AdapterHandle* out = nullptr;
Result<Adapter, FfiError> CoreDeviceProxy::create_tcp_adapter() && {
AdapterHandle *out = nullptr;
// Rust consumes the proxy regardless of result → release BEFORE call
CoreDeviceProxyHandle* raw = this->release();
// Rust consumes the proxy regardless of result → release BEFORE call
CoreDeviceProxyHandle *raw = this->release();
if (IdeviceFfiError* e = ::core_device_proxy_create_tcp_adapter(raw, &out)) {
err = FfiError(e);
return std::nullopt;
}
return Adapter::adopt(out);
FfiError e(::core_device_proxy_create_tcp_adapter(raw, &out));
if (e) {
return Err(e);
}
return Ok(Adapter::adopt(out));
}
} // namespace IdeviceFFI