@@ -97,6 +97,13 @@ impl Read for StdinRaw {
97
97
Initializer :: nop ( )
98
98
}
99
99
}
100
+ impl StdinRaw {
101
+ #[ cfg( any( unix, windows) ) ]
102
+ #[ stable( since = "1.46.0" , feature = "stdin_nonblocking" ) ]
103
+ pub unsafe fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
104
+ self . 0 . set_nonblocking ( nonblocking)
105
+ }
106
+ }
100
107
impl Write for StdoutRaw {
101
108
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
102
109
self . 0 . write ( buf)
@@ -365,6 +372,33 @@ impl Stdin {
365
372
pub fn read_line ( & self , buf : & mut String ) -> io:: Result < usize > {
366
373
self . lock ( ) . read_line ( buf)
367
374
}
375
+
376
+ /// Set the `stdin` in non-blocking mode.
377
+ ///
378
+ /// It is useful in case you're playing with termcaps. However, please note that it is
379
+ /// unsafe because it'll modify the behaviour of all other stdin.
380
+ ///
381
+ /// # Example
382
+ ///
383
+ /// ```no_run
384
+ /// use std::io::{self, Read};
385
+ ///
386
+ /// fn main() -> io::Result<()> {
387
+ /// let mut buffer = String::new();
388
+ /// let stdin = io::stdin();
389
+ /// let mut handle = stdin.lock();
390
+ ///
391
+ /// handle.set_nonblocking(true)?;
392
+ /// handle.read_to_string(&mut buffer)?;
393
+ ///
394
+ /// Ok(())
395
+ /// }
396
+ /// ```
397
+ #[ cfg( any( unix, windows) ) ]
398
+ #[ stable( since = "1.46.0" , feature = "stdin_nonblocking" ) ]
399
+ pub unsafe fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
400
+ self . lock ( ) . set_nonblocking ( nonblocking)
401
+ }
368
402
}
369
403
370
404
#[ stable( feature = "std_debug" , since = "1.16.0" ) ]
@@ -439,6 +473,38 @@ impl fmt::Debug for StdinLock<'_> {
439
473
}
440
474
}
441
475
476
+ impl StdinLock < ' _ > {
477
+ /// Set the `stdin` in non-blocking mode.
478
+ ///
479
+ /// It is useful in case you're playing with termcaps. However, please note that it is
480
+ /// unsafe because it'll modify the behaviour of all other stdin.
481
+ ///
482
+ /// # Example
483
+ ///
484
+ /// ```no_run
485
+ /// use std::io::{self, Read};
486
+ ///
487
+ /// fn main() -> io::Result<()> {
488
+ /// let mut buffer = String::new();
489
+ /// let stdin = io::stdin();
490
+ /// let mut handle = stdin.lock();
491
+ ///
492
+ /// handle.set_nonblocking(true)?;
493
+ /// handle.read_to_string(&mut buffer)?;
494
+ ///
495
+ /// Ok(())
496
+ /// }
497
+ /// ```
498
+ #[ cfg( any( unix, windows) ) ]
499
+ #[ stable( since = "1.46.0" , feature = "stdin_nonblocking" ) ]
500
+ pub unsafe fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
501
+ match self . inner . get_ref ( ) {
502
+ Maybe :: Real ( ref stdin) => stdin. set_nonblocking ( nonblocking) ,
503
+ Maybe :: Fake => Ok ( ( ) ) ,
504
+ }
505
+ }
506
+ }
507
+
442
508
/// A handle to the global standard output stream of the current process.
443
509
///
444
510
/// Each handle shares a global buffer of data to be written to the standard
0 commit comments