• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::future::Future;
15 use std::io;
16 use std::io::Write;
17 use std::pin::Pin;
18 use std::task::{Context, Poll};
19 
20 use crate::io::{AsyncWrite, State};
21 use crate::spawn_blocking;
22 
23 /// A handle to the global standard err stream of the current process.
24 ///
25 /// `Stderr` implements the [`AsyncWrite`] trait.
26 pub struct Stderr {
27     std: Option<io::Stderr>,
28     state: State<io::Stderr>,
29     has_written: bool,
30 }
31 
32 /// Constructs a new handle to the standard output of the current process.
33 ///
34 /// # Example
35 /// ```
36 /// use ylong_runtime::io::stderr;
37 /// let _stderr = stderr();
38 /// ```
stderr() -> Stderr39 pub fn stderr() -> Stderr {
40     let std = io::stderr();
41     Stderr {
42         std: Some(std),
43         state: State::init(),
44         has_written: false,
45     }
46 }
47 
48 impl AsyncWrite for Stderr {
49     crate::io::stdio::std_async_write!();
50 }
51 
52 #[cfg(unix)]
53 use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
54 
55 #[cfg(unix)]
56 impl AsRawFd for Stderr {
as_raw_fd(&self) -> RawFd57     fn as_raw_fd(&self) -> RawFd {
58         io::stdout().as_raw_fd()
59     }
60 }
61 
62 #[cfg(unix)]
63 impl AsFd for Stderr {
as_fd(&self) -> BorrowedFd<'_>64     fn as_fd(&self) -> BorrowedFd<'_> {
65         unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
66     }
67 }
68 
69 #[cfg(windows)]
70 use std::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle, RawHandle};
71 
72 #[cfg(windows)]
73 impl AsRawHandle for Stderr {
as_raw_handle(&self) -> RawHandle74     fn as_raw_handle(&self) -> RawHandle {
75         io::stdout().as_raw_handle()
76     }
77 }
78 
79 #[cfg(windows)]
80 impl AsHandle for Stderr {
as_handle(&self) -> BorrowedHandle<'_>81     fn as_handle(&self) -> BorrowedHandle<'_> {
82         unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
83     }
84 }
85