wlan_common/
sink.rs

1// Copyright 2018 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use futures::channel::mpsc;
6
7#[derive(Debug)]
8pub struct UnboundedSink<T> {
9    sink: mpsc::UnboundedSender<T>,
10}
11
12// Manually impl clone to allow non-Clone values of T.
13// https://github.com/rust-lang/rust/issues/26925
14impl<T> Clone for UnboundedSink<T> {
15    fn clone(&self) -> Self {
16        Self { sink: self.sink.clone() }
17    }
18}
19
20impl<T> UnboundedSink<T> {
21    pub fn new(sink: mpsc::UnboundedSender<T>) -> Self {
22        UnboundedSink { sink }
23    }
24
25    pub fn send(&self, msg: T) {
26        match self.sink.unbounded_send(msg) {
27            Ok(()) => {}
28            Err(e) => {
29                if e.is_full() {
30                    panic!("Did not expect an unbounded channel to be full: {:?}", e);
31                }
32                // If the other side has disconnected, we can still technically function,
33                // so ignore the error.
34            }
35        }
36    }
37}