Shl in std::ops - Rust (original) (raw)

logo

Trait std::ops::Shl

1.0.0· source · [−]

pub trait Shl<Rhs = Self> {
    type Output;
    fn shl(self, rhs: Rhs) -> Self::Output;
}

Expand description

The left shift operator <<. Note that because this trait is implemented for all integer types with multiple right-hand-side types, Rust’s type checker has special handling for _ << _, setting the result type for integer operations to the type of the left-hand-side operand. This means that though a << b and a.shl(b) are one and the same from an evaluation standpoint, they are different when it comes to type inference.

An implementation of Shl that lifts the << operation on integers to a wrapper around usize.

use std::ops::Shl;

#[derive(PartialEq, Debug)]
struct Scalar(usize);

impl Shl<Scalar> for Scalar {
    type Output = Self;

    fn shl(self, Self(rhs): Self) -> Self::Output {
        let Self(lhs) = self;
        Self(lhs << rhs)
    }
}

assert_eq!(Scalar(4) << Scalar(2), Scalar(16));

Run

An implementation of Shl that spins a vector leftward by a given amount.

use std::ops::Shl;

#[derive(PartialEq, Debug)]
struct SpinVector<T: Clone> {
    vec: Vec<T>,
}

impl<T: Clone> Shl<usize> for SpinVector<T> {
    type Output = Self;

    fn shl(self, rhs: usize) -> Self::Output {
        // Rotate the vector by `rhs` places.
        let (a, b) = self.vec.split_at(rhs);
        let mut spun_vector = vec![];
        spun_vector.extend_from_slice(b);
        spun_vector.extend_from_slice(a);
        Self { vec: spun_vector }
    }
}

assert_eq!(SpinVector { vec: vec![0, 1, 2, 3, 4] } << 2,
           SpinVector { vec: vec![2, 3, 4, 0, 1] });

Run

The resulting type after applying the << operator.

Performs the << operation.

assert_eq!(5u8 << 1, 10);
assert_eq!(1u8 << 1, 2);

Run

impl<'_, '_> Shl<&'_ i8> for &'_ i8

impl<'_, '_> Shl<&'_ i8> for &'_ u8

impl<'_, '_> Shl<&'_ u8> for &'_ i8

impl<'_, '_> Shl<&'_ u8> for &'_ u8

impl<'_, T, const LANES: usize> Shl<&'_ Simd<T, LANES>> for Simd<T, LANES> where

T: SimdElement,
Simd<T, LANES>: Shl<Simd<T, LANES>>,
LaneCount: SupportedLaneCount,
<Simd<T, LANES> as Shl<Simd<T, LANES>>>::Output == Simd<T, LANES>,

impl<'_, T, const LANES: usize> Shl<Simd<T, LANES>> for &'_ Simd<T, LANES> where

T: SimdElement,
Simd<T, LANES>: Shl<Simd<T, LANES>>,
LaneCount: SupportedLaneCount,
<Simd<T, LANES> as Shl<Simd<T, LANES>>>::Output == Simd<T, LANES>,

impl<'lhs, 'rhs, T, const LANES: usize> Shl<&'rhs Simd<T, LANES>> for &'lhs Simd<T, LANES> where

T: SimdElement,
Simd<T, LANES>: Shl<Simd<T, LANES>>,
LaneCount: SupportedLaneCount,
<Simd<T, LANES> as Shl<Simd<T, LANES>>>::Output == Simd<T, LANES>,