Skip to content

Commit

Permalink
feat: add better message when version mismatch detected
Browse files Browse the repository at this point in the history
  • Loading branch information
daulet committed Nov 5, 2024
1 parent ae6604c commit 7d2cce6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::path::PathBuf;
use std::ptr;
use tokenizers::tokenizer::Tokenizer;

const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");

#[repr(C)]
pub struct TokenizerOptions {
encode_special_tokens: bool,
Expand All @@ -19,6 +21,11 @@ pub struct Buffer {
len: usize,
}

#[no_mangle]
pub extern "C" fn version() -> *const libc::c_char {
std::ffi::CString::new(CARGO_PKG_VERSION).unwrap().into_raw()
}

#[no_mangle]
pub extern "C" fn from_bytes(bytes: *const u8, len: u32, opts: &TokenizerOptions) -> *mut Tokenizer {
let bytes_slice = unsafe { std::slice::from_raw_parts(bytes, len as usize) };
Expand Down
11 changes: 11 additions & 0 deletions tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ import "C"

// NOTE: There should be NO space between the comments and the `import "C"` line.
import (
"fmt"
"io"
"unsafe"
)

const WANT_VERSION = "0.20.2"

func init() {
version := C.version()
got := C.GoString(version)
if got != WANT_VERSION {
panic(fmt.Errorf("tokenizers library version mismatch, want: %s, got: %s", WANT_VERSION, got))
}
}

type Tokenizer struct {
tokenizer unsafe.Pointer
}
Expand Down
2 changes: 2 additions & 0 deletions tokenizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct Buffer {
uint32_t len;
};

const char *version();

void *from_bytes(const uint8_t *config, uint32_t len, const struct TokenizerOptions *options);

void *from_bytes_with_truncation(const uint8_t *config, uint32_t len, uint32_t max_len, uint8_t direction);
Expand Down

0 comments on commit 7d2cce6

Please sign in to comment.