diff --git a/src/main.rs b/src/main.rs index 423e268..12bf672 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,7 @@ use actix_web::http::{header, StatusCode}; use std::io; use std::collections::HashMap; use std::io::{Read, BufRead, Write, ErrorKind}; -use futures::{Stream, StreamExt, TryStreamExt, future, stream, TryFutureExt}; +use futures::{Stream, StreamExt, TryStreamExt, future, stream, TryFutureExt, pin_mut}; use actix_web::web::{Buf, Bytes}; use actix_web::http::header::IntoHeaderValue; @@ -243,31 +243,48 @@ async fn git_proto(payload : web::Payload, web::Path((owner, reponame)): web::Pa //let mut body = Vec::new(); //rdr.bytes() - let response = builder.streaming(stream::try_unfold(0, |_useless| async move { - let mut buff : [u8; 1024] = [0; 1024]; - rdr.read(&mut buff[..]).await.map(|l| { - print!("{}", String::from_utf8_lossy(&buff[0..l])); - Some(Bytes::copy_from_slice(&buff[0..l])) - }); - }) - ); + // let stream = stream::try_unfold(0, |state| async move { + // + // if state <= 2 { + // let next_state = state + 1; + // let yielded = state * 2; + // Ok(Some((yielded, next_state))) + // } else { + // Ok(None) + // } + // }); - let response = builder.streaming(stream::repeat_with(move || { - let mut buff : [u8; 1024] = [0; 1024]; - match rdr.read(&mut buff[..]) { - Ok(l) => { - print!("{}", String::from_utf8_lossy(&buff[0..l])); - Ok(Bytes::copy_from_slice(&buff[0..l])) } + let unfold = stream::try_unfold(0, |_useless| async move { + let mut buff: [u8; 1024] = [0; 1024]; + let result = rdr.read(&mut buff[..]).await; + match result { + Ok(l) => {Ok(Some((Bytes::copy_from_slice(&buff[0..l]), 0)))} Err(e) => {Err(e)} } - }) - .take_while(|bytes| { - match bytes { - Ok(bytes) => {future::ready(bytes.len() != 0)} - Err(_) => {future::ready(false)} - } - }) - ); + // result.map(|l| { + // print!("{}", String::from_utf8_lossy(&buff[0..l])); + // Some((Bytes::copy_from_slice(&buff[0..l]), 0)) + // }); + }); + pin_mut!(unfold); + let response = builder.streaming(unfold); + + // let response = builder.streaming(stream::repeat_with(move || { + // let mut buff : [u8; 1024] = [0; 1024]; + // match rdr.read(&mut buff[..]) { + // Ok(l) => { + // print!("{}", String::from_utf8_lossy(&buff[0..l])); + // Ok(Bytes::copy_from_slice(&buff[0..l])) } + // Err(e) => {Err(e)} + // } + // }) + // .take_while(|bytes| { + // match bytes { + // Ok(bytes) => {future::ready(bytes.len() != 0)} + // Err(_) => {future::ready(false)} + // } + // }) + // ); return Ok(response); }