Skip to content

Commit

Permalink
fix: use macro to simplify defining Either type
Browse files Browse the repository at this point in the history
  • Loading branch information
ChieloNewctle committed Oct 19, 2023
1 parent ce59713 commit c0c48a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod sam;
pub mod trie;
pub mod utils;

use pyo3::prelude::*;

Expand Down
16 changes: 5 additions & 11 deletions src/sam.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
extern crate general_sam as general_sam_rs;

use crate::trie::Trie;

use std::{str::from_utf8, sync::Arc};

use either::{
for_both, Either as CharOrByte,
{Either::Left as CharSide, Either::Right as ByteSide},
};
use pyo3::{prelude::*, types::PyDict};
use crate::utils::{char_or_byte_type, for_both, ByteSide, CharSide};

use general_sam_rs::{sam as sam_rs, trie as trie_rs, trie_alike::TravelEvent};
use pyo3::{prelude::*, types::PyDict};
use std::{str::from_utf8, sync::Arc};

type RustGeneralSAM = CharOrByte<sam_rs::GeneralSAM<char>, sam_rs::GeneralSAM<u8>>;
type RustGeneralSAMState<'s> =
CharOrByte<sam_rs::GeneralSAMState<'s, char>, sam_rs::GeneralSAMState<'s, u8>>;
type RustGeneralSAM = char_or_byte_type!(sam_rs::GeneralSAM);
type RustGeneralSAMState<'s> = char_or_byte_type!(sam_rs::GeneralSAMState; 's);

#[pyclass]
pub struct GeneralSAM(pub Arc<RustGeneralSAM>);
Expand Down
14 changes: 5 additions & 9 deletions src/trie.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
extern crate general_sam as general_sam_rs;

use std::{convert::Infallible, str::from_utf8};

use either::{
for_both, Either as CharOrByte,
{Either::Left as CharSide, Either::Right as ByteSide},
};
use pyo3::prelude::*;
use crate::utils::{char_or_byte_type, for_both, ByteSide, CharSide};

use general_sam_rs::{
trie as trie_rs,
trie_alike::{TravelEvent, TrieNodeAlike},
};
use pyo3::prelude::*;
use std::{convert::Infallible, str::from_utf8};

type RustTrie = CharOrByte<trie_rs::Trie<char>, trie_rs::Trie<u8>>;
type RustTrieNode = CharOrByte<trie_rs::TrieNode<char>, trie_rs::TrieNode<u8>>;
type RustTrie = char_or_byte_type!(trie_rs::Trie);
type RustTrieNode = char_or_byte_type!(trie_rs::TrieNode);

#[pyclass]
pub struct Trie(pub RustTrie);
Expand Down
16 changes: 16 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pub use either::{
for_both, Either as CharOrByte,
{Either::Left as CharSide, Either::Right as ByteSide},
};

#[macro_export]
macro_rules! char_or_byte_type {
($( $name:ident )::+ $(; $( $life:lifetime ),* )?) => {
$crate::utils::CharOrByte<
$($name)::+ < $( $($life),* ,)? char>,
$($name)::+ < $( $($life),* ,)? u8>,
>
};
}

pub use char_or_byte_type;

0 comments on commit c0c48a2

Please sign in to comment.