1 #![cfg_attr(target_os = "wasi", feature(wasi_ext))]
2 #![cfg(feature = "close")]
3
4 use io_lifetimes::raw::{AsRawFilelike, AsRawSocketlike};
5 use io_lifetimes::views::{FilelikeView, SocketlikeView};
6 use io_lifetimes::{
7 AsFilelike, AsSocketlike, BorrowedFilelike, FromFilelike, FromSocketlike, IntoFilelike,
8 IntoSocketlike,
9 };
10 use std::io::{Read, Write};
11
12 struct Tester {}
13 impl Tester {
use_file<Filelike: AsFilelike>(filelike: Filelike)14 fn use_file<Filelike: AsFilelike>(filelike: Filelike) {
15 let mut buf = Vec::new();
16
17 let filelike = filelike.as_filelike();
18
19 let view = filelike.as_filelike_view::<std::fs::File>();
20 let _ = (&*view).read(&mut buf).is_ok();
21 let _ = (&*view).write(&buf).is_ok();
22
23 let view = unsafe {
24 FilelikeView::<std::fs::File>::view_raw(
25 filelike
26 .as_filelike_view::<std::fs::File>()
27 .as_raw_filelike(),
28 )
29 };
30 let _ = (&*view).read(&mut buf).is_ok();
31 let _ = (&*view).write(&buf).is_ok();
32
33 let _ = dbg!(filelike);
34 }
35
use_socket<Socketlike: AsSocketlike>(socketlike: Socketlike)36 fn use_socket<Socketlike: AsSocketlike>(socketlike: Socketlike) {
37 let mut buf = Vec::new();
38
39 let socketlike = socketlike.as_socketlike();
40 let view = socketlike.as_socketlike_view::<std::net::TcpStream>();
41 let _ = (&*view).read(&mut buf).is_ok();
42 let _ = (&*view).write(&buf).is_ok();
43
44 let view = unsafe {
45 SocketlikeView::<std::net::TcpStream>::view_raw(
46 socketlike
47 .as_socketlike_view::<std::net::TcpStream>()
48 .as_raw_socketlike(),
49 )
50 };
51 let _ = (&*view).read(&mut buf).is_ok();
52 let _ = (&*view).write(&buf).is_ok();
53
54 let _ = dbg!(socketlike);
55 }
56
from_file<Filelike: IntoFilelike>(filelike: Filelike)57 fn from_file<Filelike: IntoFilelike>(filelike: Filelike) {
58 let mut buf = Vec::new();
59
60 let filelike = filelike.into_filelike();
61 let view = filelike.as_filelike_view::<std::fs::File>();
62 let _ = (&*view).read(&mut buf).is_ok();
63 let _ = (&*view).write(&buf).is_ok();
64 drop(view);
65
66 let _ = dbg!(&filelike);
67 let _ = std::fs::File::from_filelike(filelike);
68 }
69
from_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike)70 fn from_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) {
71 let mut buf = Vec::new();
72
73 let socketlike = socketlike.into_socketlike();
74 let view = socketlike.as_socketlike_view::<std::net::TcpStream>();
75 let _ = (&*view).read(&mut buf).is_ok();
76 let _ = (&*view).write(&buf).is_ok();
77 drop(view);
78
79 let _ = dbg!(&socketlike);
80 let _ = std::net::TcpStream::from_socketlike(socketlike);
81 }
82
from_into_file<Filelike: IntoFilelike>(filelike: Filelike)83 fn from_into_file<Filelike: IntoFilelike>(filelike: Filelike) {
84 let _ = std::fs::File::from_into_filelike(filelike);
85 }
86
from_into_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike)87 fn from_into_socket<Socketlike: IntoSocketlike>(socketlike: Socketlike) {
88 let _ = std::net::TcpStream::from_into_socketlike(socketlike);
89 }
90 }
91
92 #[test]
test_api()93 fn test_api() {
94 let file = std::fs::File::open("Cargo.toml").unwrap();
95 Tester::use_file(&file);
96 Tester::use_file(file.as_filelike());
97 Tester::use_file(&*file.as_filelike_view::<std::fs::File>());
98 Tester::use_file(file.as_filelike_view::<std::fs::File>().as_filelike());
99
100 let socket = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
101 Tester::use_socket(&socket);
102 Tester::use_socket(socket.as_socketlike());
103 Tester::use_socket(&*socket.as_socketlike_view::<std::net::TcpListener>());
104 Tester::use_socket(
105 socket
106 .as_socketlike_view::<std::net::TcpListener>()
107 .as_socketlike(),
108 );
109
110 Tester::from_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike());
111 Tester::from_file(
112 std::fs::File::open("Cargo.toml")
113 .unwrap()
114 .into_filelike()
115 .into_filelike(),
116 );
117 Tester::from_socket(
118 std::net::TcpListener::bind("127.0.0.1:0")
119 .unwrap()
120 .into_socketlike(),
121 );
122 Tester::from_socket(
123 std::net::TcpListener::bind("127.0.0.1:0")
124 .unwrap()
125 .into_socketlike()
126 .into_socketlike(),
127 );
128
129 Tester::from_into_file(std::fs::File::open("Cargo.toml").unwrap().into_filelike());
130 Tester::from_into_socket(
131 std::net::TcpListener::bind("127.0.0.1:0")
132 .unwrap()
133 .into_socketlike(),
134 );
135 }
136
137 #[test]
test_as()138 fn test_as() {
139 let file = std::fs::File::open("Cargo.toml").unwrap();
140 let borrow: BorrowedFilelike = file.as_filelike();
141 let reborrow: BorrowedFilelike = borrow.as_filelike();
142 let ref_reborrow: &BorrowedFilelike = &reborrow;
143 let borrow_ref_reborrow: BorrowedFilelike = ref_reborrow.as_filelike();
144 let _ref_borrow_ref_reborrow: &BorrowedFilelike = &borrow_ref_reborrow;
145 }
146