Support bool and char literals as arguments to const generics.

This commit is contained in:
Jason Orendorff
2022-05-10 17:11:42 -05:00
committed by Emilio Cobos Álvarez
parent eb5c979912
commit caf69c8800
21 changed files with 578 additions and 0 deletions
+8
View File
@@ -340,6 +340,14 @@ impl ConstExpr {
lit: syn::Lit::Int(ref len),
..
}) => Ok(ConstExpr::Value(len.base10_digits().to_string())),
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Bool(syn::LitBool { value, .. }),
..
}) => Ok(ConstExpr::Value(value.to_string())),
syn::Expr::Lit(syn::ExprLit {
lit: syn::Lit::Char(ref ch),
..
}) => Ok(ConstExpr::Value(u32::to_string(&ch.value().into()))),
syn::Expr::Path(ref path) => {
let generic_path = GenericPath::load(&path.path)?;
Ok(ConstExpr::Name(generic_path.export_name().to_owned()))
@@ -0,0 +1,36 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
typedef struct HashTable_Str__c_char__false {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
} HashTable_Str__c_char__false;
typedef struct HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
typedef struct HashTable_Str__u64__true {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
} HashTable_Str__u64__true;
typedef void (*MapCallback)(Str key, uint64_t val);
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
struct HashTable_Str__u64__true *new_map(void);
void map_for_each(const struct HashTable_Str__u64__true *map, MapCallback callback);
@@ -0,0 +1,44 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
typedef struct HashTable_Str__c_char__false {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
} HashTable_Str__c_char__false;
typedef struct HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
typedef struct HashTable_Str__u64__true {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
} HashTable_Str__u64__true;
typedef void (*MapCallback)(Str key, uint64_t val);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
struct HashTable_Str__u64__true *new_map(void);
void map_for_each(const struct HashTable_Str__u64__true *map, MapCallback callback);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+36
View File
@@ -0,0 +1,36 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
typedef struct {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
} HashTable_Str__c_char__false;
typedef HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
typedef struct {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
} HashTable_Str__u64__true;
typedef void (*MapCallback)(Str key, uint64_t val);
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
HashTable_Str__u64__true *new_map(void);
void map_for_each(const HashTable_Str__u64__true *map, MapCallback callback);
@@ -0,0 +1,44 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
typedef struct {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
} HashTable_Str__c_char__false;
typedef HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
typedef struct {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
} HashTable_Str__u64__true;
typedef void (*MapCallback)(Str key, uint64_t val);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
HashTable_Str__u64__true *new_map(void);
void map_for_each(const HashTable_Str__u64__true *map, MapCallback callback);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,37 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
template<typename T = void>
struct MaybeUninit;
using Str = const char*;
template<typename K, typename V, bool IS_MAP>
struct HashTable {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
MaybeUninit<K> *keys;
MaybeUninit<V> *vals;
};
using MySet = HashTable<Str, char, false>;
using SetCallback = void(*)(Str key);
using MapCallback = void(*)(Str key, uint64_t val);
extern "C" {
MySet *new_set();
void set_for_each(const MySet *set, SetCallback callback);
HashTable<Str, uint64_t, true> *new_map();
void map_for_each(const HashTable<Str, uint64_t, true> *map, MapCallback callback);
} // extern "C"
@@ -0,0 +1,37 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
ctypedef const char *Str;
ctypedef struct HashTable_Str__c_char__false:
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
ctypedef HashTable_Str__c_char__false MySet;
ctypedef void (*SetCallback)(Str key);
ctypedef struct HashTable_Str__u64__true:
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
ctypedef void (*MapCallback)(Str key, uint64_t val);
MySet *new_set();
void set_for_each(const MySet *set, SetCallback callback);
HashTable_Str__u64__true *new_map();
void map_for_each(const HashTable_Str__u64__true *map, MapCallback callback);
@@ -0,0 +1,36 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
struct HashTable_Str__c_char__false {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
};
typedef struct HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
struct HashTable_Str__u64__true {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
};
typedef void (*MapCallback)(Str key, uint64_t val);
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
struct HashTable_Str__u64__true *new_map(void);
void map_for_each(const struct HashTable_Str__u64__true *map, MapCallback callback);
@@ -0,0 +1,44 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef const char *Str;
struct HashTable_Str__c_char__false {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
};
typedef struct HashTable_Str__c_char__false MySet;
typedef void (*SetCallback)(Str key);
struct HashTable_Str__u64__true {
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
};
typedef void (*MapCallback)(Str key, uint64_t val);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
MySet *new_set(void);
void set_for_each(const MySet *set, SetCallback callback);
struct HashTable_Str__u64__true *new_map(void);
void map_for_each(const struct HashTable_Str__u64__true *map, MapCallback callback);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,37 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
ctypedef const char *Str;
cdef struct HashTable_Str__c_char__false:
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
char *vals;
ctypedef HashTable_Str__c_char__false MySet;
ctypedef void (*SetCallback)(Str key);
cdef struct HashTable_Str__u64__true:
uintptr_t num_buckets;
uintptr_t capacity;
uint8_t *occupied;
Str *keys;
uint64_t *vals;
ctypedef void (*MapCallback)(Str key, uint64_t val);
MySet *new_set();
void set_for_each(const MySet *set, SetCallback callback);
HashTable_Str__u64__true *new_map();
void map_for_each(const HashTable_Str__u64__true *map, MapCallback callback);
@@ -0,0 +1,12 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct TakeUntil_0 {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
} TakeUntil_0;
struct TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct TakeUntil_0 {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
} TakeUntil_0;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
struct TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+12
View File
@@ -0,0 +1,12 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
} TakeUntil_0;
TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
} TakeUntil_0;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,18 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
template<uint32_t V>
struct TakeUntil {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
};
extern "C" {
TakeUntil<0> until_nul(const uint8_t *start, uintptr_t len);
} // extern "C"
@@ -0,0 +1,14 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
ctypedef struct TakeUntil_0:
const uint8_t *start;
uintptr_t len;
uintptr_t point;
TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
@@ -0,0 +1,12 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct TakeUntil_0 {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
};
struct TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
@@ -0,0 +1,20 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
struct TakeUntil_0 {
const uint8_t *start;
uintptr_t len;
uintptr_t point;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
struct TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,14 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
cdef struct TakeUntil_0:
const uint8_t *start;
uintptr_t len;
uintptr_t point;
TakeUntil_0 until_nul(const uint8_t *start, uintptr_t len);
+57
View File
@@ -0,0 +1,57 @@
use std::mem::MaybeUninit;
use libc::c_char;
#[repr(C)]
pub struct HashTable<K, V, const IS_MAP: bool> {
num_buckets: usize,
capacity: usize,
occupied: *mut u8,
keys: *mut MaybeUninit<K>,
vals: *mut MaybeUninit<V>,
}
type Str = *const c_char;
pub type HashMap<K, V> = HashTable<K, V, true>;
pub type HashSet<K> = HashTable<K, u8, false>;
impl<K, V, const IS_MAP: bool> HashTable<K, V, IS_MAP>
{
pub fn new() -> Self {
HashTable {
num_buckets: 0,
capacity: 0,
occupied: std::ptr::null_mut(),
keys: std::ptr::null_mut(),
vals: std::ptr::null_mut(),
}
}
}
// with alias
type MySet = HashTable<Str, c_char, false>;
#[no_mangle]
pub extern "C" fn new_set() -> *mut MySet {
Box::into_raw(Box::new(HashSet::new()))
}
type SetCallback = unsafe extern "C" fn(key: Str);
#[no_mangle]
pub unsafe extern "C" fn set_for_each(set: *const MySet, callback: SetCallback) {
todo!();
}
// without alias
#[no_mangle]
pub extern "C" fn new_map() -> *mut HashTable<Str, u64, true> {
Box::into_raw(Box::new(HashMap::new()))
}
type MapCallback = unsafe extern "C" fn(key: Str, val: u64);
#[no_mangle]
pub unsafe extern "C" fn map_for_each(map: *const HashTable<Str, u64, true>, callback: MapCallback) {
todo!();
}
+20
View File
@@ -0,0 +1,20 @@
use std::marker::PhantomData;
#[repr(C)]
struct TakeUntil<'a, const V: char>
{
marker: PhantomData<&'a str>,
start: *const u8,
len: usize,
point: usize,
}
#[no_mangle]
pub unsafe extern "C" fn until_nul(start: *const u8, len: usize) -> TakeUntil<'a, '\0'> {
TakeUntil {
marker: PhantomData,
start,
len,
point: 0,
}
}