You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: bits.rs
    path of file: ./repos/snapshot/src
    
    the code of the file until where you have to start completion: // Axel '0vercl0k' Souchet - January 21 2024
use std::mem;
use std::ops::RangeInclusive;

/// Utility trait to make it easier to extract ranges of bits.
pub trait Bits: Sized {
    /// Get a range of bits.
    fn bits(&self, r: RangeInclusive<usize>) -> Self;

    /// Get a bit.
    fn bit(&self, n: usize) -> Self {
        self.bits(n..=n)
    }
}

impl<T> Bits for T
where
    T: Into<u128> + Copy + TryFrom<u128>,
    <T as TryFrom<u128>>::Error: std::fmt::Debug,
{
    
    
    fn bits(&self, r: RangeInclusive<usize>) -> Self {
        let (start, end) = r.into_inner();
        let capacity = mem::size_of_val(self) * 8;
        assert!(start <= end, "the range should have a start <= end");
        assert!(
            end < capacity,
            "the end ({end}) of the range can't exceed the bits capacity ({capacity}) of Self"
        );
        let value = (*self).into();
        let base = value >> start;
        let n = end - start + 1;

        let mask = if n == capacity {
            // This prevents to overflow a u128 when doing `(1 << 128) - 1`
            !0
        } else {
            (1 << n) - 1
        };

        // This cannot fail as we are zero extending `Self` into a `u128` and then the
        // `mask` cannot index outside the bit capacity of `Self`.
        T::try_from(base & mask).unwrap()
    }