11a0216d1Sopenharmony_ci#![cfg_attr(target_os = "wasi", feature(wasi_ext))] 21a0216d1Sopenharmony_ci#![cfg(feature = "close")] 31a0216d1Sopenharmony_ci 41a0216d1Sopenharmony_ciuse io_lifetimes::raw::{AsRawFilelike, AsRawSocketlike}; 51a0216d1Sopenharmony_ciuse io_lifetimes::views::{FilelikeView, SocketlikeView}; 61a0216d1Sopenharmony_ciuse io_lifetimes::{ 71a0216d1Sopenharmony_ci AsFilelike, AsSocketlike, BorrowedFilelike, FromFilelike, FromSocketlike, IntoFilelike, 81a0216d1Sopenharmony_ci IntoSocketlike, 91a0216d1Sopenharmony_ci}; 101a0216d1Sopenharmony_ciuse std::io::{Read, Write}; 111a0216d1Sopenharmony_ci 121a0216d1Sopenharmony_cistruct Tester {} 131a0216d1Sopenharmony_ciimpl Tester { 141a0216d1Sopenharmony_ci fn use_file<Filelike: AsFilelike>(filelike: Filelike) { 151a0216d1Sopenharmony_ci let mut buf = Vec::new(); 161a0216d1Sopenharmony_ci 171a0216d1Sopenharmony_ci let filelike = filelike.as_filelike(); 181a0216d1Sopenharmony_ci 191a0216d1Sopenharmony_ci let view = filelike.as_filelike_view::<std::fs::File>(); 201a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 211a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 221a0216d1Sopenharmony_ci 231a0216d1Sopenharmony_ci let view = unsafe { 241a0216d1Sopenharmony_ci FilelikeView::<std::fs::File>::view_raw( 251a0216d1Sopenharmony_ci filelike 261a0216d1Sopenharmony_ci .as_filelike_view::<std::fs::File>() 271a0216d1Sopenharmony_ci .as_raw_filelike(), 281a0216d1Sopenharmony_ci ) 291a0216d1Sopenharmony_ci }; 301a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 311a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 321a0216d1Sopenharmony_ci 331a0216d1Sopenharmony_ci let _ = dbg!(filelike); 341a0216d1Sopenharmony_ci } 351a0216d1Sopenharmony_ci 361a0216d1Sopenharmony_ci fn use_socket<Socketlike: AsSocketlike>(socketlike: Socketlike) { 371a0216d1Sopenharmony_ci let mut buf = Vec::new(); 381a0216d1Sopenharmony_ci 391a0216d1Sopenharmony_ci let socketlike = socketlike.as_socketlike(); 401a0216d1Sopenharmony_ci let view = socketlike.as_socketlike_view::<std::net::TcpStream>(); 411a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 421a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 431a0216d1Sopenharmony_ci 441a0216d1Sopenharmony_ci let view = unsafe { 451a0216d1Sopenharmony_ci SocketlikeView::<std::net::TcpStream>::view_raw( 461a0216d1Sopenharmony_ci socketlike 471a0216d1Sopenharmony_ci .as_socketlike_view::<std::net::TcpStream>() 481a0216d1Sopenharmony_ci .as_raw_socketlike(), 491a0216d1Sopenharmony_ci ) 501a0216d1Sopenharmony_ci }; 511a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 521a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 531a0216d1Sopenharmony_ci 541a0216d1Sopenharmony_ci let _ = dbg!(socketlike); 551a0216d1Sopenharmony_ci } 561a0216d1Sopenharmony_ci 571a0216d1Sopenharmony_ci fn from_file<Filelike: IntoFilelike>(filelike: Filelike) { 581a0216d1Sopenharmony_ci let mut buf = Vec::new(); 591a0216d1Sopenharmony_ci 601a0216d1Sopenharmony_ci let filelike = filelike.into_filelike(); 611a0216d1Sopenharmony_ci let view = filelike.as_filelike_view::<std::fs::File>(); 621a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 631a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 641a0216d1Sopenharmony_ci drop(view); 651a0216d1Sopenharmony_ci 661a0216d1Sopenharmony_ci let _ = dbg!(&filelike); 671a0216d1Sopenharmony_ci let _ = std::fs::File::from_filelike(filelike); 681a0216d1Sopenharmony_ci } 691a0216d1Sopenharmony_ci 701a0216d1Sopenharmony_ci fn from_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) { 711a0216d1Sopenharmony_ci let mut buf = Vec::new(); 721a0216d1Sopenharmony_ci 731a0216d1Sopenharmony_ci let socketlike = socketlike.into_socketlike(); 741a0216d1Sopenharmony_ci let view = socketlike.as_socketlike_view::<std::net::TcpStream>(); 751a0216d1Sopenharmony_ci let _ = (&*view).read(&mut buf).is_ok(); 761a0216d1Sopenharmony_ci let _ = (&*view).write(&buf).is_ok(); 771a0216d1Sopenharmony_ci drop(view); 781a0216d1Sopenharmony_ci 791a0216d1Sopenharmony_ci let _ = dbg!(&socketlike); 801a0216d1Sopenharmony_ci let _ = std::net::TcpStream::from_socketlike(socketlike); 811a0216d1Sopenharmony_ci } 821a0216d1Sopenharmony_ci 831a0216d1Sopenharmony_ci fn from_into_file<Filelike: IntoFilelike>(filelike: Filelike) { 841a0216d1Sopenharmony_ci let _ = std::fs::File::from_into_filelike(filelike); 851a0216d1Sopenharmony_ci } 861a0216d1Sopenharmony_ci 871a0216d1Sopenharmony_ci fn from_into_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) { 881a0216d1Sopenharmony_ci let _ = std::net::TcpStream::from_into_socketlike(socketlike); 891a0216d1Sopenharmony_ci } 901a0216d1Sopenharmony_ci} 911a0216d1Sopenharmony_ci 921a0216d1Sopenharmony_ci#[test] 931a0216d1Sopenharmony_cifn test_api() { 941a0216d1Sopenharmony_ci let file = std::fs::File::open("Cargo.toml").unwrap(); 951a0216d1Sopenharmony_ci Tester::use_file(&file); 961a0216d1Sopenharmony_ci Tester::use_file(file.as_filelike()); 971a0216d1Sopenharmony_ci Tester::use_file(&*file.as_filelike_view::<std::fs::File>()); 981a0216d1Sopenharmony_ci Tester::use_file(file.as_filelike_view::<std::fs::File>().as_filelike()); 991a0216d1Sopenharmony_ci 1001a0216d1Sopenharmony_ci let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); 1011a0216d1Sopenharmony_ci Tester::use_socket(&socket); 1021a0216d1Sopenharmony_ci Tester::use_socket(socket.as_socketlike()); 1031a0216d1Sopenharmony_ci Tester::use_socket(&*socket.as_socketlike_view::<std::net::TcpListener>()); 1041a0216d1Sopenharmony_ci Tester::use_socket( 1051a0216d1Sopenharmony_ci socket 1061a0216d1Sopenharmony_ci .as_socketlike_view::<std::net::TcpListener>() 1071a0216d1Sopenharmony_ci .as_socketlike(), 1081a0216d1Sopenharmony_ci ); 1091a0216d1Sopenharmony_ci 1101a0216d1Sopenharmony_ci Tester::from_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike()); 1111a0216d1Sopenharmony_ci Tester::from_file( 1121a0216d1Sopenharmony_ci std::fs::File::open("Cargo.toml") 1131a0216d1Sopenharmony_ci .unwrap() 1141a0216d1Sopenharmony_ci .into_filelike() 1151a0216d1Sopenharmony_ci .into_filelike(), 1161a0216d1Sopenharmony_ci ); 1171a0216d1Sopenharmony_ci Tester::from_socket( 1181a0216d1Sopenharmony_ci std::net::TcpListener::bind("127.0.0.1:0") 1191a0216d1Sopenharmony_ci .unwrap() 1201a0216d1Sopenharmony_ci .into_socketlike(), 1211a0216d1Sopenharmony_ci ); 1221a0216d1Sopenharmony_ci Tester::from_socket( 1231a0216d1Sopenharmony_ci std::net::TcpListener::bind("127.0.0.1:0") 1241a0216d1Sopenharmony_ci .unwrap() 1251a0216d1Sopenharmony_ci .into_socketlike() 1261a0216d1Sopenharmony_ci .into_socketlike(), 1271a0216d1Sopenharmony_ci ); 1281a0216d1Sopenharmony_ci 1291a0216d1Sopenharmony_ci Tester::from_into_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike()); 1301a0216d1Sopenharmony_ci Tester::from_into_socket( 1311a0216d1Sopenharmony_ci std::net::TcpListener::bind("127.0.0.1:0") 1321a0216d1Sopenharmony_ci .unwrap() 1331a0216d1Sopenharmony_ci .into_socketlike(), 1341a0216d1Sopenharmony_ci ); 1351a0216d1Sopenharmony_ci} 1361a0216d1Sopenharmony_ci 1371a0216d1Sopenharmony_ci#[test] 1381a0216d1Sopenharmony_cifn test_as() { 1391a0216d1Sopenharmony_ci let file = std::fs::File::open("Cargo.toml").unwrap(); 1401a0216d1Sopenharmony_ci let borrow: BorrowedFilelike = file.as_filelike(); 1411a0216d1Sopenharmony_ci let reborrow: BorrowedFilelike = borrow.as_filelike(); 1421a0216d1Sopenharmony_ci let ref_reborrow: &BorrowedFilelike = &reborrow; 1431a0216d1Sopenharmony_ci let borrow_ref_reborrow: BorrowedFilelike = ref_reborrow.as_filelike(); 1441a0216d1Sopenharmony_ci let _ref_borrow_ref_reborrow: &BorrowedFilelike = &borrow_ref_reborrow; 1451a0216d1Sopenharmony_ci} 146