pub struct Slice<Prim: PrimitiveType = Num> { /* private fields */ }
Expand description

Slice type. Unlike in Rust, slices are a subset of tuples. If length is exact (has no UnknownLen part), the slice is completely equivalent to the corresponding tuple.

Notation

A slice is denoted as [T; N] where T is the slice element and N is the slice length. A special case is [T], a slice with a dynamic length.

Examples

use arithmetic_parser::grammars::{F32Grammar, Parse};
use arithmetic_typing::{Annotated, TupleLen, TypeEnvironment, Type};

type Parser = Annotated<F32Grammar>;
let ast = Parser::parse_statements("xs: [Num; _] = (1, 2, 3);")?;
let mut env = TypeEnvironment::new();
env.process_statements(&ast)?;
// Slices with fixed length are equivalent to tuples.
assert_eq!(env["xs"].to_string(), "(Num, Num, Num)");

let code = "
    xs: [Num] = (1, 2, 3);
    ys = xs + 1; // works fine: despite `xs` having unknown length,
                 // it's always possible to add a number to it
    (_, _, z) = xs; // does not work: the tuple length is erased
";
let ast = Parser::parse_statements(code)?;
let errors = env.process_statements(&ast).unwrap_err();

let err = errors.iter().next().unwrap();
assert_eq!(err.main_location().span(code), "(_, _, z)");
assert_eq!(env["ys"], env["xs"]);

Implementations§

source§

impl<Prim: PrimitiveType> Slice<Prim>

source

pub fn new(element: Type<Prim>, length: impl Into<TupleLen>) -> Self

Creates a new slice.

source

pub fn element(&self) -> &Type<Prim>

Returns the element type of this slice.

source

pub fn len(&self) -> TupleLen

Returns the length of this slice.

source

pub fn is_empty(&self) -> bool

Returns true iff this slice is definitely empty.

Trait Implementations§

source§

impl<Prim: Clone + PrimitiveType> Clone for Slice<Prim>

source§

fn clone(&self) -> Slice<Prim>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Prim: Debug + PrimitiveType> Debug for Slice<Prim>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> Display for Slice<Prim>

source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Prim: PrimitiveType> From<Slice<Prim>> for Tuple<Prim>

source§

fn from(slice: Slice<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PrimitiveType> From<Slice<Prim>> for Type<Prim>

source§

fn from(slice: Slice<Prim>) -> Self

Converts to this type from the input type.
source§

impl<Prim: PartialEq + PrimitiveType> PartialEq<Slice<Prim>> for Slice<Prim>

source§

fn eq(&self, other: &Slice<Prim>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<Prim: PrimitiveType> StructuralPartialEq for Slice<Prim>

Auto Trait Implementations§

§

impl<Prim = Num> !RefUnwindSafe for Slice<Prim>

§

impl<Prim> Send for Slice<Prim>

§

impl<Prim> Sync for Slice<Prim>

§

impl<Prim> Unpin for Slice<Prim>

§

impl<Prim = Num> !UnwindSafe for Slice<Prim>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.