Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Generics with particular type "instantiation" prevention
Now I'm trying to write a (yet another) YAML parser.
YAML has several context for several items.
nb-double-text
is defined for 4 contexts out of 6.I want to make this context non-runtime parameter but have no idea what's a good approach. I considered a few approaches but all seem not good enough.
Context
.
Can anyone suggest me a good approach? Thanks in advance
1 implementation example
enum YamlContext {FlowIn, FlowOut, ...}
fn parse_nb_double_text<const ctx: YamlContext>(input: &mut &str) -> Result<&str>;
// how can I define this for only FlowIn/FlowOut/BlockKey/FlowKey??
2 implementation example
trait YamlContext: DoubleTextParse {}
struct FlowIn {}
impl YamlContext for FlowIn {}
fn parse_nb_double_text<Ctx: YamlContext>(input: &mut &str) -> Result<&str> {
<Ctx as DoubleTextParse>::parse(input)
}
trait DoubleTextParse {
fn parse(...);
}
// FIXME: I need to impl DoubleTextParse for all context, while I don't need it for BlockIn/BlockOut.
3 example
// define all _$context variants.
fn parse_nb_double_text_flow_in(...)
fn parse_nb_double_text_flow_out(...)
fn parse_nb_double_text_flow_key(...)
fn parse_nb_double_text_block_key(...)
// problem is that forwarding context is cumbersome and error-prone.
1 post - 1 participant
🏷️ Rust_feed