char/avril
(wip) why'd you have to go and make things so complicaaaaated?
git clone https://git.t4t.associates/char/avril
f4a0f5b
main
1use avril_core:: serde_v8:: JsBuffer ; 2use std:: io; 3 4pub const CHUNK : usize =64 * 1024 ; 5/// Portable per-operation segment limit: the POSIX minimum vector count. 6pub const MAX_SLICES : usize =16 ; 7pub type Response < T > =( Option < T >, Option <( &' static str , String )>); 8 9pub fn response < T >( result : io:: Result < T >) ->Response < T > { 10match result{ 11Ok ( value) =>( Some ( value), None ), 12Err ( error) =>{ 13let kind =match error. kind () { 14 io:: ErrorKind :: NotFound =>"NotFound" , 15 io:: ErrorKind :: PermissionDenied =>"PermissionDenied" , 16 io:: ErrorKind :: AlreadyExists =>"AlreadyExists" , 17 io:: ErrorKind :: DirectoryNotEmpty =>"NotEmpty" , 18 io:: ErrorKind :: CrossesDevices =>"CrossDevice" , 19 io:: ErrorKind :: NotADirectory =>"NotADirectory" , 20 io:: ErrorKind :: IsADirectory =>"IsADirectory" , 21 io:: ErrorKind :: InvalidInput =>"InvalidInput" , 22 io:: ErrorKind :: NotConnected =>"BadResource" , 23 io:: ErrorKind :: WouldBlock =>"Busy" , 24 io:: ErrorKind :: BrokenPipe =>"BrokenPipe" , 25 io:: ErrorKind :: Unsupported =>"NotSupported" , 26 _ =>"IoError" , 27}; 28( None , Some (( kind, error. to_string ()))) 29} 30} 31} 32 33/// Keeps a vector's leading bounded prefix; the rest reports as a partial 34/// write. Nonempty data is never skipped or reordered. 35pub fn bound_vectored ( buffers : Vec < JsBuffer >) ->Vec < JsBuffer > { 36let mut kept =Vec :: new (); 37let mut total =0 ; 38for mut segmentin buffers{ 39if kept. len () >=MAX_SLICES || total >=CHUNK { 40break ; 41} 42if segment. is_empty () { 43continue ; 44} 45let room =CHUNK - total; 46if segment. len () > room{ 47let mut slice = segment. into_parts (); 48 slice. truncate ( room); 49 segment =JsBuffer :: from_parts ( slice); 50} 51 total += segment. len (); 52 kept. push ( segment); 53} 54 kept 55}