feat(napi) implement Either3 to Either26.

This patch introduces a new macro: `either_n!` that generates the types
`Either{n}` where $3 \leq n \leq 26$. Manual implementations for
`Either3`, `Either4` and `Either5` are removed by this patch too.

The `either_n!` macro is quite classical. There is no particular
trick, except `count_idents!` which simply turns, e.g. `A, B, C` into
`3`. This macro is used by `either_n!` to implement the
`debug_assert!` inside `from_napi_value`.
This commit is contained in:
Ivan Enderlin 2022-06-01 21:47:23 +02:00 committed by LongYinan
parent 496fc3e54a
commit 81b07ce5a6
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
2 changed files with 112 additions and 221 deletions

View file

@ -180,6 +180,27 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("Either3", "{} | {} | {}"), ("Either3", "{} | {} | {}"),
("Either4", "{} | {} | {} | {}"), ("Either4", "{} | {} | {} | {}"),
("Either5", "{} | {} | {} | {} | {}"), ("Either5", "{} | {} | {} | {} | {}"),
("Either6", "{} | {} | {} | {} | {} | {}"),
("Either7", "{} | {} | {} | {} | {} | {} | {}"),
("Either8", "{} | {} | {} | {} | {} | {} | {} | {}"),
("Either9", "{} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either10", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either11", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either12", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either13", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either14", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either15", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either16", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either17", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either18", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either19", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either20", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either21", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either22", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either23", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either24", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either25", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Either26", "{} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {}"),
("Null", "null"), ("Null", "null"),
("JsNull", "null"), ("JsNull", "null"),
("null", "null"), ("null", "null"),

View file

@ -95,16 +95,30 @@ impl<A: ToNapiValue, B: ToNapiValue> ToNapiValue for Either<A, B> {
} }
} }
#[derive(Debug, Clone, Copy)] macro_rules! count_idents {
pub enum Either3<A, B, C> { ( $( $idents:ident ),* $( , )* ) => {
A(A), {
B(B), #[allow(dead_code, non_camel_case_types)]
C(C), enum Idents { $( $idents, )* __LastVariant }
const COUNT: usize = Idents::__LastVariant as usize;
COUNT
}
};
} }
impl<A: TypeName, B: TypeName, C: TypeName> TypeName for Either3<A, B, C> { macro_rules! either_n {
( $either_name:ident, $( $parameter:ident ),+ $( , )* ) => {
#[derive(Debug, Clone, Copy)]
pub enum $either_name< $( $parameter ),+ > {
$( $parameter ( $parameter ) ),+
}
impl< $( $parameter ),+ > TypeName for $either_name < $( $parameter ),+ >
where $( $parameter: TypeName ),+
{
fn type_name() -> &'static str { fn type_name() -> &'static str {
"Either3" stringify!( $either_name )
} }
fn value_type() -> ValueType { fn value_type() -> ValueType {
@ -112,221 +126,77 @@ impl<A: TypeName, B: TypeName, C: TypeName> TypeName for Either3<A, B, C> {
} }
} }
impl<A: TypeName + FromNapiValue, B: TypeName + FromNapiValue, C: TypeName + FromNapiValue> impl< $( $parameter ),+ > FromNapiValue for $either_name < $( $parameter ),+ >
FromNapiValue for Either3<A, B, C> where $( $parameter: TypeName + FromNapiValue ),+
{ {
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> { unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
debug_assert!( debug_assert!(
{ {
let mut types = vec![A::value_type(), B::value_type(), C::value_type()]; let mut types = vec![ $( $parameter ::value_type() ),+ ];
types.dedup(); types.dedup();
types.len() == 3
types.len() == count_idents!( $( $parameter ),+ )
}, },
"{}", "{}",
ERROR_MSG ERROR_MSG
); );
let js_type = type_of!(env, napi_val)?; let js_type = type_of!(env, napi_val)?;
if js_type == A::value_type() {
unsafe { A::from_napi_value(env, napi_val).map(Self::A) } $(
} else if js_type == B::value_type() { if js_type == $parameter::value_type() {
unsafe { B::from_napi_value(env, napi_val).map(Self::B) } unsafe { $parameter ::from_napi_value(env, napi_val).map(Self:: $parameter ) }
} else if js_type == C::value_type() { } else
unsafe { C::from_napi_value(env, napi_val).map(Self::C) } )+
} else { {
Err(crate::Error::new( Err(crate::Error::new(
Status::InvalidArg, Status::InvalidArg,
format!( format!(
"Expect type {} or {} or {}, but got {}", concat!("Expect type ", $( "`{", stringify!( $parameter ), "}`, " ),+ , "but got {js_type}"),
A::value_type(), $( $parameter = $parameter::value_type(), )+
B::value_type(), js_type = js_type,
C::value_type(),
js_type
), ),
)) ))
} }
} }
} }
impl<A: ToNapiValue, B: ToNapiValue, C: ToNapiValue> ToNapiValue for Either3<A, B, C> { impl< $( $parameter ),+ > ToNapiValue for $either_name < $( $parameter ),+ >
unsafe fn to_napi_value( where $( $parameter: ToNapiValue ),+
env: sys::napi_env,
value: Self,
) -> crate::Result<crate::sys::napi_value> {
match value {
Self::A(a) => unsafe { A::to_napi_value(env, a) },
Self::B(b) => unsafe { B::to_napi_value(env, b) },
Self::C(c) => unsafe { C::to_napi_value(env, c) },
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Either4<A, B, C, D> {
A(A),
B(B),
C(C),
D(D),
}
impl<A: TypeName, B: TypeName, C: TypeName, D: TypeName> TypeName for Either4<A, B, C, D> {
fn type_name() -> &'static str {
"Either4"
}
fn value_type() -> ValueType {
ValueType::Unknown
}
}
impl<
A: TypeName + FromNapiValue,
B: TypeName + FromNapiValue,
C: TypeName + FromNapiValue,
D: TypeName + FromNapiValue,
> FromNapiValue for Either4<A, B, C, D>
{
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
debug_assert!(
{
let mut types = vec![
A::value_type(),
B::value_type(),
C::value_type(),
D::value_type(),
];
types.dedup();
types.len() == 4
},
"{}",
ERROR_MSG
);
let js_type = type_of!(env, napi_val)?;
if js_type == A::value_type() {
unsafe { A::from_napi_value(env, napi_val).map(Self::A) }
} else if js_type == B::value_type() {
unsafe { B::from_napi_value(env, napi_val).map(Self::B) }
} else if js_type == C::value_type() {
unsafe { C::from_napi_value(env, napi_val).map(Self::C) }
} else if js_type == D::value_type() {
unsafe { D::from_napi_value(env, napi_val).map(Self::D) }
} else {
Err(crate::Error::new(
Status::InvalidArg,
format!(
"Expect type {} or {} or {} or {}, but got {}",
A::value_type(),
B::value_type(),
C::value_type(),
D::value_type(),
js_type
),
))
}
}
}
impl<A: ToNapiValue, B: ToNapiValue, C: ToNapiValue, D: ToNapiValue> ToNapiValue
for Either4<A, B, C, D>
{ {
unsafe fn to_napi_value( unsafe fn to_napi_value(
env: sys::napi_env, env: sys::napi_env,
value: Self, value: Self
) -> crate::Result<crate::sys::napi_value> { ) -> crate::Result<crate::sys::napi_value> {
match value { match value {
Self::A(a) => unsafe { A::to_napi_value(env, a) }, $( Self:: $parameter (v) => unsafe { $parameter ::to_napi_value(env, v) } ),+
Self::B(b) => unsafe { B::to_napi_value(env, b) },
Self::C(c) => unsafe { C::to_napi_value(env, c) },
Self::D(d) => unsafe { D::to_napi_value(env, d) },
} }
} }
} }
};
}
#[derive(Debug, Clone, Copy)] either_n!(Either3, A, B, C);
pub enum Either5<A, B, C, D, E> { either_n!(Either4, A, B, C, D);
A(A), either_n!(Either5, A, B, C, D, E);
B(B), either_n!(Either6, A, B, C, D, E, F);
C(C), either_n!(Either7, A, B, C, D, E, F, G);
D(D), either_n!(Either8, A, B, C, D, E, F, G, H);
E(E), either_n!(Either9, A, B, C, D, E, F, G, H, I);
} either_n!(Either10, A, B, C, D, E, F, G, H, I, J);
either_n!(Either11, A, B, C, D, E, F, G, H, I, J, K);
impl<A: TypeName, B: TypeName, C: TypeName, D: TypeName, E: TypeName> TypeName either_n!(Either12, A, B, C, D, E, F, G, H, I, J, K, L);
for Either5<A, B, C, D, E> either_n!(Either13, A, B, C, D, E, F, G, H, I, J, K, L, M);
{ either_n!(Either14, A, B, C, D, E, F, G, H, I, J, K, L, M, N);
fn type_name() -> &'static str { either_n!(Either15, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O);
"Either5" either_n!(Either16, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P);
} either_n!(Either17, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q);
either_n!(Either18, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R);
fn value_type() -> ValueType { either_n!(Either19, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S);
ValueType::Unknown either_n!(Either20, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T);
} either_n!(Either21, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U);
} either_n!(Either22, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V);
either_n!(Either23, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W);
impl< either_n!(Either24, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X);
A: TypeName + FromNapiValue, either_n!(Either25, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y);
B: TypeName + FromNapiValue, either_n!(Either26, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);
C: TypeName + FromNapiValue,
D: TypeName + FromNapiValue,
E: TypeName + FromNapiValue,
> FromNapiValue for Either5<A, B, C, D, E>
{
unsafe fn from_napi_value(env: sys::napi_env, napi_val: sys::napi_value) -> crate::Result<Self> {
debug_assert!(
{
let mut types = vec![
A::value_type(),
B::value_type(),
C::value_type(),
D::value_type(),
E::value_type(),
];
types.dedup();
types.len() == 5
},
"{}",
ERROR_MSG
);
let js_type = type_of!(env, napi_val)?;
if js_type == A::value_type() {
unsafe { A::from_napi_value(env, napi_val).map(Self::A) }
} else if js_type == B::value_type() {
unsafe { B::from_napi_value(env, napi_val).map(Self::B) }
} else if js_type == C::value_type() {
unsafe { C::from_napi_value(env, napi_val).map(Self::C) }
} else if js_type == D::value_type() {
unsafe { D::from_napi_value(env, napi_val).map(Self::D) }
} else if js_type == E::value_type() {
unsafe { E::from_napi_value(env, napi_val).map(Self::E) }
} else {
Err(crate::Error::new(
Status::InvalidArg,
format!(
"Expect type {} or {} or {} or {} or {}, but got {}",
A::value_type(),
B::value_type(),
C::value_type(),
D::value_type(),
E::value_type(),
js_type
),
))
}
}
}
impl<A: ToNapiValue, B: ToNapiValue, C: ToNapiValue, D: ToNapiValue, E: ToNapiValue> ToNapiValue
for Either5<A, B, C, D, E>
{
unsafe fn to_napi_value(
env: sys::napi_env,
value: Self,
) -> crate::Result<crate::sys::napi_value> {
match value {
Self::A(a) => unsafe { A::to_napi_value(env, a) },
Self::B(b) => unsafe { B::to_napi_value(env, b) },
Self::C(c) => unsafe { C::to_napi_value(env, c) },
Self::D(d) => unsafe { D::to_napi_value(env, d) },
Self::E(e) => unsafe { E::to_napi_value(env, e) },
}
}
}