Use state for callbacks

This commit is contained in:
Jackson Coxson
2025-02-06 15:49:17 -07:00
parent 5ccfe2d46a
commit fdf68d3d5a
2 changed files with 20 additions and 11 deletions

View File

@@ -89,16 +89,18 @@ impl Idevice {
/// Sends raw bytes to the socket
async fn send_raw(&mut self, message: &[u8]) -> Result<(), IdeviceError> {
self.send_raw_with_progress(message, |_| async {}).await
self.send_raw_with_progress(message, |_| async {}, ()).await
}
async fn send_raw_with_progress<Fut>(
async fn send_raw_with_progress<Fut, S>(
&mut self,
message: &[u8],
callback: impl Fn((usize, usize)) -> Fut,
callback: impl Fn(((usize, usize), S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
if let Some(socket) = &mut self.socket {
let message_parts = message.chunks(1024 * 64);
@@ -107,7 +109,7 @@ impl Idevice {
for (i, part) in message_parts.enumerate() {
debug!("Writing {i}/{part_len}");
socket.write_all(part).await?;
callback((i, part_len)).await;
callback(((i, part_len), state.clone())).await;
}
Ok(())
} else {

View File

@@ -80,19 +80,21 @@ impl ImageMounter {
image: &[u8],
signature: Vec<u8>,
) -> Result<(), IdeviceError> {
self.upload_image_with_progress(image_type, image, signature, |_| async {})
self.upload_image_with_progress(image_type, image, signature, |_| async {}, ())
.await
}
pub async fn upload_image_with_progress<Fut>(
pub async fn upload_image_with_progress<Fut, S>(
&mut self,
image_type: impl Into<String>,
image: &[u8],
signature: Vec<u8>,
callback: impl Fn((usize, usize)) -> Fut,
callback: impl Fn(((usize, usize), S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
let image_type = image_type.into();
let image_size = match u64::try_from(image.len()) {
@@ -124,7 +126,9 @@ impl ImageMounter {
}
debug!("Sending image bytes");
self.idevice.send_raw_with_progress(image, callback).await?;
self.idevice
.send_raw_with_progress(image, callback, state)
.await?;
let res = self.idevice.read_plist().await?;
match res.get("Status") {
@@ -328,6 +332,7 @@ impl ImageMounter {
info_plist,
unique_chip_id,
|_| async {},
(),
)
.await
}
@@ -335,7 +340,7 @@ impl ImageMounter {
/// Calling this has the potential of closing the socket,
/// so a provider is required for this abstraction.
#[allow(clippy::too_many_arguments)] // literally nobody asked
pub async fn mount_personalized_with_callback<Fut>(
pub async fn mount_personalized_with_callback<Fut, S>(
&mut self,
provider: &dyn crate::provider::IdeviceProvider,
image: Vec<u8>,
@@ -343,10 +348,12 @@ impl ImageMounter {
build_manifest: &[u8],
info_plist: Option<plist::Value>,
unique_chip_id: u64,
callback: impl Fn((usize, usize)) -> Fut,
callback: impl Fn(((usize, usize), S)) -> Fut,
state: S,
) -> Result<(), IdeviceError>
where
Fut: std::future::Future<Output = ()>,
S: Clone,
{
// Try to fetch personalization manifest
let mut hasher = Sha384::new();
@@ -371,7 +378,7 @@ impl ImageMounter {
};
debug!("Uploading imaage");
self.upload_image_with_progress("Personalized", &image, manifest.clone(), callback)
self.upload_image_with_progress("Personalized", &image, manifest.clone(), callback, state)
.await?;
debug!("Mounting image");