fn try_send(&self, msg: T) -> Result<usize, TrySendError<T>> {
        let backoff = Backoff::new();
        let mut tail = self.tail.load(Ordering::Relaxed);

        loop {
            // Extract mark bit from the tail and unset it.
            //
            // If the mark bit was set (which means all receivers have been dropped), we will still
            // send the message into the channel if there is enough capacity. The message will get
            // dropped when the channel is dropped (which means when all senders are also dropped).
            let mark_bit = tail & self.mark_bit;
            tail ^= mark_bit;

            // Deconstruct the tail.
            let index = tail & (self.mark_bit - 1);
            let lap = tail & !(self.one_lap - 1);

            // Inspect the corresponding slot.
            let slot = unsafe { &*self.buffer.add(index) };
            let stamp = slot.stamp.load(Ordering::Acquire);

            // If the tail and the stamp match, we may attempt to push.
            if tail == stamp {
                let new_tail = if index + 1 < self.cap {
                    // Same lap, incremented index.
                    // Set to `{ lap: lap, mark: 0, index: index + 1 }`.
                    tail + 1
                } else {
                    // One lap forward, index wraps around to zero.
                    // Set to `{ lap: lap.wrapping_add(1), mark: 0, index: 0 }`.
                    lap.wrapping_add(self.one_lap)
                };

                // Try moving the tail.
                match self.tail.compare_exchange_weak(
                    tail | mark_bit,
                    new_tail | mark_bit,
                    Ordering::SeqCst,
                    Ordering::Relaxed,
                ) {
                    Ok(_) => {
                        // Write the message into the slot and update the stamp.
                        unsafe { slot.msg.get().write(msg) };
                        let stamp = tail + 1;
                        slot.stamp.store(stamp, Ordering::Release);

                        // Wake a blocked receive operation.
                        self.recv_ops.notify_one();

                        // Wake all blocked streams.
                        self.next_ops.notify_all();

                        return Ok(stamp);
                    }
                    Err(t) => {
                        tail = t;
                        backoff.spin();
                    }
                }
            } else if stamp.wrapping_add(self.one_lap) == tail + 1 {
                atomic::fence(Ordering::SeqCst);
                let head = self.head.load(Ordering::Relaxed);

                // If the head lags one lap behind the tail as well...
                if head.wrapping_add(self.one_lap) == tail {
                    // ...then the channel is full.

                    // Check if the channel is disconnected.
                    if mark_bit != 0 {
                        return Err(TrySendError::Disconnected(msg));
                    } else {
                        return Err(TrySendError::Full(msg));
                    }
                }

                backoff.spin();
                tail = self.tail.load(Ordering::Relaxed);
            } else {
                // Snooze because we need to wait for the stamp to get updated.
                backoff.snooze();
                tail = self.tail.load(Ordering::Relaxed);
            }
        }
    }
/// Attempts to send a message.
Attempts to send a message.
Attempts to send a message .
fn try_send(&self, msg: T) -> Result<usize, TrySendError<T>> {
        let backoff = Backoff::new();
        let mut tail = self.tail.load(Ordering::Relaxed);
        loop {
            // Extract mark bit from the tail and unset it.
            //
            // If the mark bit was set (which means all receivers have been dropped), we will still
            // send the message into the channel if there is enough capacity. The message will get
            // dropped when the channel is dropped (which means when all senders are also dropped).
            let mark_bit = tail & self.mark_bit;
            tail ^= mark_bit;
            // Deconstruct the tail.
            let index = tail & (self.mark_bit - 1);
            let lap = tail & !(self.one_lap - 1);
            // Inspect the corresponding slot.
            let slot = unsafe { &*self.buffer.add(index) };
            let stamp = slot.stamp.load(Ordering::Acquire);
            // If the tail and the stamp match, we may attempt to push.
            if tail == stamp {
                let new_tail = if index + 1 < self.cap {
                    // Same lap, incremented index.
                    // Set to `{ lap: lap, mark: 0, index: index + 1 }`.
                    tail + 1
                } else {
                    // One lap forward, index wraps around to zero.
                    // Set to `{ lap: lap.wrapping_add(1), mark: 0, index: 0 }`.
                    lap.wrapping_add(self.one_lap)
                };
                // Try moving the tail.
                match self.tail.compare_exchange_weak(
                    tail | mark_bit,
                    new_tail | mark_bit,
                    Ordering::SeqCst,
                    Ordering::Relaxed,
                ) {
                    Ok(_) => {
                        // Write the message into the slot and update the stamp.
                        unsafe { slot.msg.get().write(msg) };
                        let stamp = tail + 1;
                        slot.stamp.store(stamp, Ordering::Release);
                        // Wake a blocked receive operation.
                        self.recv_ops.notify_one();
                        // Wake all blocked streams.
                        self.next_ops.notify_all();
                        return Ok(stamp);
                    }
                    Err(t) => {
                        tail = t;
                        backoff.spin();
                    }
                }
            } else if stamp.wrapping_add(self.one_lap) == tail + 1 {
                atomic::fence(Ordering::SeqCst);
                let head = self.head.load(Ordering::Relaxed);
                // If the head lags one lap behind the tail as well...
                if head.wrapping_add(self.one_lap) == tail {
                    // ...then the channel is full.
                    // Check if the channel is disconnected.
                    if mark_bit != 0 {
                        return Err(TrySendError::Disconnected(msg));
                    } else {
                        return Err(TrySendError::Full(msg));
                    }
                }
                backoff.spin();
                tail = self.tail.load(Ordering::Relaxed);
            } else {
                // Snooze because we need to wait for the stamp to get updated.
                backoff.snooze();
                tail = self.tail.load(Ordering::Relaxed);
            }
        }
    }
fn try_send ( & self , msg : T ) -> Result < usize , TrySendError < T > > { let backoff = Backoff :: new ( ) ; let mut tail = self . tail . load ( Ordering :: Relaxed ) ; loop { // Extract mark bit from the tail and unset it. // // If the mark bit was set (which means all receivers have been dropped), we will still // send the message into the channel if there is enough capacity. The message will get // dropped when the channel is dropped (which means when all senders are also dropped). let mark_bit = tail & self . mark_bit ; tail ^= mark_bit ; // Deconstruct the tail. let index = tail & ( self . mark_bit - 1 ) ; let lap = tail & ! ( self . one_lap - 1 ) ; // Inspect the corresponding slot. let slot = unsafe { & * self . buffer . add ( index ) } ; let stamp = slot . stamp . load ( Ordering :: Acquire ) ; // If the tail and the stamp match, we may attempt to push. if tail == stamp { let new_tail = if index + 1 < self . cap { // Same lap, incremented index. // Set to `{ lap: lap, mark: 0, index: index + 1 }`. tail + 1 } else { // One lap forward, index wraps around to zero. // Set to `{ lap: lap.wrapping_add(1), mark: 0, index: 0 }`. lap . wrapping_add ( self . one_lap ) } ; // Try moving the tail. match self . tail . compare_exchange_weak ( tail | mark_bit , new_tail | mark_bit , Ordering :: SeqCst , Ordering :: Relaxed , ) { Ok ( _ ) => { // Write the message into the slot and update the stamp. unsafe { slot . msg . get ( ) . write ( msg ) } ; let stamp = tail + 1 ; slot . stamp . store ( stamp , Ordering :: Release ) ; // Wake a blocked receive operation. self . recv_ops . notify_one ( ) ; // Wake all blocked streams. self . next_ops . notify_all ( ) ; return Ok ( stamp ) ; } Err ( t ) => { tail = t ; backoff . spin ( ) ; } } } else if stamp . wrapping_add ( self . one_lap ) == tail + 1 { atomic :: fence ( Ordering :: SeqCst ) ; let head = self . head . load ( Ordering :: Relaxed ) ; // If the head lags one lap behind the tail as well... if head . wrapping_add ( self . one_lap ) == tail { // ...then the channel is full. // Check if the channel is disconnected. if mark_bit != 0 { return Err ( TrySendError :: Disconnected ( msg ) ) ; } else { return Err ( TrySendError :: Full ( msg ) ) ; } } backoff . spin ( ) ; tail = self . tail . load ( Ordering :: Relaxed ) ; } else { // Snooze because we need to wait for the stamp to get updated. backoff . snooze ( ) ; tail = self . tail . load ( Ordering :: Relaxed ) ; } } }
Attempts to send a message.
Attempts to send a message .
&self
msg
T