@@ -7,12 +7,25 @@ use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
77use std:: rc:: Rc ;
88
99thread_local ! {
10+ static SAFE_STDIN : Value <std:: fs:: File > = Rc :: new( RefCell :: new( std:: fs:: File :: from(
11+ std:: io:: stdin( ) . as_fd( ) . try_clone_to_owned( ) . unwrap( ) ,
12+ ) ) ) ;
1013 static SAFE_STDOUT : Value <std:: fs:: File > = Rc :: new( RefCell :: new( std:: fs:: File :: from(
1114 std:: io:: stdout( ) . as_fd( ) . try_clone_to_owned( ) . unwrap( ) ,
1215 ) ) ) ;
1316 static SAFE_STDERR : Value <std:: fs:: File > = Rc :: new( RefCell :: new( std:: fs:: File :: from(
1417 std:: io:: stderr( ) . as_fd( ) . try_clone_to_owned( ) . unwrap( ) ,
1518 ) ) ) ;
19+ static UNSAFE_STDIN : UnsafeCell <std:: fs:: File > = unsafe {
20+ UnsafeCell :: new(
21+ std:: fs:: File :: from_raw_fd(
22+ std:: io:: stdin( )
23+ . as_fd( )
24+ . try_clone_to_owned( )
25+ . unwrap( )
26+ . into_raw_fd( ) ,
27+ ) )
28+ } ;
1629 static UNSAFE_STDOUT : UnsafeCell <std:: fs:: File > = unsafe {
1730 UnsafeCell :: new(
1831 std:: fs:: File :: from_raw_fd(
@@ -35,6 +48,10 @@ thread_local! {
3548 } ;
3649}
3750
51+ pub fn cin ( ) -> Ptr < std:: fs:: File > {
52+ SAFE_STDIN . with ( AsPointer :: as_pointer)
53+ }
54+
3855pub fn cout ( ) -> Ptr < std:: fs:: File > {
3956 SAFE_STDOUT . with ( AsPointer :: as_pointer)
4057}
@@ -43,6 +60,14 @@ pub fn cerr() -> Ptr<std::fs::File> {
4360 SAFE_STDERR . with ( AsPointer :: as_pointer)
4461}
4562
63+ /// # Safety
64+ ///
65+ /// The caller must ensure that the returned pointer is not used after the
66+ // thread finishes.
67+ pub unsafe fn cin_unsafe ( ) -> * mut std:: fs:: File {
68+ UNSAFE_STDIN . with ( UnsafeCell :: get)
69+ }
70+
4671/// # Safety
4772///
4873/// The caller must ensure that the returned pointer is not used after the
0 commit comments