From d0229fbcfb11666b5abf7ae487ffe335866f9326 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Fri, 11 Oct 2024 17:30:49 -0700 Subject: [PATCH] get #[hdl] struct S to work --- crates/fayalite/src/array.rs | 2 +- crates/fayalite/src/int.rs | 51 ++++++++++++++++++------------ crates/fayalite/tests/hdl_types.rs | 4 +-- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/crates/fayalite/src/array.rs b/crates/fayalite/src/array.rs index a3aea79..f617f91 100644 --- a/crates/fayalite/src/array.rs +++ b/crates/fayalite/src/array.rs @@ -85,7 +85,7 @@ impl ArrayType { } } -impl ArrayType { +impl> ArrayType { pub fn new_static(element: T) -> Self { Self::new(element, Len::SizeType::default()) } diff --git a/crates/fayalite/src/int.rs b/crates/fayalite/src/int.rs index dd631d0..03b2c88 100644 --- a/crates/fayalite/src/int.rs +++ b/crates/fayalite/src/int.rs @@ -32,8 +32,23 @@ mod sealed { pub const DYN_SIZE: usize = !0; pub type DynSize = ConstUsize; -pub trait KnownSize: GenericConstUsize + Size { +pub trait KnownSize: + GenericConstUsize + sealed::SizeTypeSealed + sealed::SizeSealed + Default +{ const SIZE: Self; + type ArrayMatch: AsRef<[Expr]> + + AsMut<[Expr]> + + BorrowMut<[Expr]> + + 'static + + Send + + Sync + + Eq + + Clone + + std::hash::Hash + + std::fmt::Debug + + IntoIterator> + + TryFrom>> + + Into>>; } macro_rules! known_widths { @@ -44,6 +59,7 @@ macro_rules! known_widths { v }> { const SIZE: Self = Self; + type ArrayMatch = [Expr; Self::VALUE]; } }; ([2 $($rest:tt)*] $($bits:literal)+) => { @@ -55,6 +71,7 @@ macro_rules! known_widths { known_widths!([$($rest)*] 1); impl KnownSize for ConstUsize<{2 $(* $rest)*}> { const SIZE: Self = Self; + type ArrayMatch = [Expr; Self::VALUE]; } }; } @@ -124,30 +141,24 @@ impl sealed::SizeSealed for ConstUsize {} impl sealed::SizeTypeSealed for ConstUsize {} -impl SizeType for ConstUsize -where - ConstUsize: KnownSize, -{ - type Size = ConstUsize; +impl SizeType for T { + type Size = T; } -impl Size for ConstUsize -where - ConstUsize: KnownSize, -{ - type ArrayMatch = [Expr; VALUE]; +impl Size for T { + type ArrayMatch = ::ArrayMatch; - const KNOWN_VALUE: Option = Some(VALUE); + const KNOWN_VALUE: Option = Some(T::VALUE); - type SizeType = ConstUsize; + type SizeType = T; fn as_usize(_size_type: Self::SizeType) -> usize { - VALUE + T::VALUE } fn try_from_usize(v: usize) -> Option { - if v == VALUE { - Some(ConstUsize) + if v == T::VALUE { + Some(T::SIZE) } else { None } @@ -252,9 +263,7 @@ macro_rules! impl_int { impl $name { pub fn new_static() -> Self { - Self { - width: Width::SizeType::default(), - } + Self { width: Width::SIZE } } } @@ -526,9 +535,9 @@ pub trait BoolOrIntType: Type + sealed::BoolOrIntTypeSealed { fn new(width: ::SizeType) -> Self; fn new_static() -> Self where - Self::Width: KnownSize, + Self::Width: KnownSize + Size, { - Self::new(::SizeType::default()) + Self::new(Self::Width::default()) } fn as_same_width_sint(self) -> SIntType { SIntType::new(Self::Width::from_usize(self.width())) diff --git a/crates/fayalite/tests/hdl_types.rs b/crates/fayalite/tests/hdl_types.rs index 71450fc..4509146 100644 --- a/crates/fayalite/tests/hdl_types.rs +++ b/crates/fayalite/tests/hdl_types.rs @@ -1,11 +1,10 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // See Notices.txt for copyright information -use fayalite::prelude::*; -#[cfg(todo)] use fayalite::{ bundle::BundleType, enum_::EnumType, int::{BoolOrIntType, IntType}, + prelude::*, ty::StaticType, }; use std::marker::PhantomData; @@ -121,7 +120,6 @@ mod bound_kind { macro_rules! check_bounds { ($name:ident<$(#[$field:ident, $kind:ident] $var:ident: $($bound:ident +)*),*>) => { - #[cfg(todo)] #[hdl(outline_generated)] struct $name<$($var: $($bound +)*,)*> { $($field: bound_kind::$kind<$var>,)*