Skip to main content

maplike/impls/
scalars.rs

1// SPDX-FileCopyrightText: 2026 maplike contributors
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5use crate::containers::Container;
6use crate::iter::IntoIter;
7use crate::ops::{Assign, Get, Len, Modify, Put, Set, WithOne};
8
9macro_rules! impl_traits_for_scalar {
10    ($($t:ty),*) => {
11        $(
12            impl Container for $t {
13                type Key = usize;
14                type Value = Self;
15            }
16
17            impl WithOne<$t> for $t {
18                #[inline(always)]
19                fn with_one(value: Self) -> Self {
20                    value
21                }
22            }
23
24            impl Assign for $t {
25                #[inline(always)]
26                fn assign(&mut self, value: Self) {
27                    *self = value;
28                }
29            }
30
31            impl Get<usize> for $t {
32                #[inline(always)]
33                fn get(&self, index: &usize) -> Option<&Self> {
34                    if *index == 0 { Some(&self) } else { None }
35                }
36            }
37
38            impl Set<usize> for $t {
39                type Output = Option<Self>;
40
41                #[inline(always)]
42                fn set(&mut self, index: usize, value: Self) -> Option<Self> {
43                    assert_eq!(index, 0);
44                    Some(core::mem::replace(self, value))
45                }
46            }
47
48            impl Modify<usize> for $t {
49                #[inline(always)]
50                fn modify<F>(&mut self, index: &usize, f: F) where F: FnOnce(&mut Self) {
51                    assert_eq!(*index, 0);
52                    f(self)
53                }
54            }
55
56            impl Put<$t> for $t {
57                #[inline(always)]
58                fn put(&mut self, value: Self) -> Option<Self> {
59                    Some(core::mem::replace(self, value))
60                }
61            }
62
63            impl Len for $t {
64                #[inline(always)]
65                fn len(&self) -> usize {
66                    1
67                }
68            }
69
70            impl IntoIter<usize> for $t {
71                type IntoIter = core::iter::Enumerate<core::iter::Once<Self>>;
72
73                #[inline(always)]
74                fn into_iter(self) -> Self::IntoIter {
75                    core::iter::once(self).enumerate()
76                }
77            }
78        )*
79    };
80}
81
82impl_traits_for_scalar!(i8, i16, i32, i64, i128, isize);
83impl_traits_for_scalar!(u8, u16, u32, u64, u128, usize);
84impl_traits_for_scalar!(f32, f64);
85impl_traits_for_scalar!(char, bool, ());