Struct arithmetic_typing::Slice
source · 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>
impl<Prim: PrimitiveType> Slice<Prim>
Trait Implementations§
source§impl<Prim: PrimitiveType> Display for Slice<Prim>
impl<Prim: PrimitiveType> Display for Slice<Prim>
source§impl<Prim: PartialEq + PrimitiveType> PartialEq for Slice<Prim>
impl<Prim: PartialEq + PrimitiveType> PartialEq for Slice<Prim>
impl<Prim: PrimitiveType> StructuralPartialEq for Slice<Prim>
Auto Trait Implementations§
impl<Prim> Freeze for Slice<Prim>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)