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

View File

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