### Arc::try_clone_from_ref Example Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Example usage of the experimental try_clone_from_ref API for Arc. ```rust #![feature(clone_from_ref)] #![feature(allocator_api)] use std::sync::Arc; let hello: Arc = Arc::try_clone_from_ref("hello")?; ``` -------------------------------- ### Quick start with CJClassifier Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/index.html Demonstrates loading the bundled classifier and detecting the language of sample strings. ```rust use cjclassifier::{CJClassifier, CJLanguage}; let classifier = CJClassifier::load_bundled().unwrap(); assert_eq!(classifier.detect("今天天气很好"), CJLanguage::ChineseSimplified); assert_eq!(classifier.detect("ひらがな"), CJLanguage::Japanese); ``` -------------------------------- ### Arc::clone_from_ref Example Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Example usage of the experimental clone_from_ref API for Arc. ```rust #![feature(clone_from_ref)] use std::sync::Arc; let hello: Arc = Arc::clone_from_ref("hello"); ``` -------------------------------- ### Get mutable reference to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Retrieves a mutable reference if the Arc is unique, otherwise returns None. ```rust use std::sync::Arc; let mut x = Arc::new(3); *Arc::get_mut(&mut x).unwrap() = 4; assert_eq!(*x, 4); let _y = Arc::clone(&x); assert!(Arc::get_mut(&mut x).is_none()); ``` -------------------------------- ### Detecting language from C/C++ Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/index.html Demonstrates loading a bundled model, performing language detection on a UTF-8 string, and freeing the classifier handle. ```c #include "cjclassifier.h" CJClassifierHandle *model = cj_load_bundled(); if (model) { const char *text = "今天天气很好"; CJLanguageCode lang = cj_detect(model, text, strlen(text)); // lang == CJ_CHINESE_SIMPLIFIED (1) cj_free(model); } ``` -------------------------------- ### Creating a New Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Constructs a new Arc instance containing the provided data. ```rust use std::sync::Arc; let five = Arc::new(5); ``` -------------------------------- ### Results::new Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.Results.html Creates a new instance of the Results struct. ```APIDOC ## pub fn new() -> Self ### Description Creates a new instance of the Results struct with default values. ### Returns - **Self** - A new instance of the Results struct. ``` -------------------------------- ### CJClassifier::load_bundled Source: https://docs.rs/cjclassifier Initializes a new CJClassifier instance by loading the bundled language models. ```APIDOC ## CJClassifier::load_bundled() ### Description Loads the bundled unigram and bigram log-probability models for CJK language detection. ### Signature `pub fn load_bundled() -> Result` ### Returns - **CJClassifier** - An instance of the classifier ready for text detection. ``` -------------------------------- ### CJClassifier::load_bundled Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/index.html Initializes the classifier by loading bundled language models. ```APIDOC ## fn load_bundled() ### Description Loads the bundled unigram and bigram log-probability models to initialize the classifier. ### Signature `pub fn load_bundled() -> Result` ### Returns - **CJClassifier** - The initialized classifier instance. ``` -------------------------------- ### cj_load_file Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_load_file.html Loads a model from a file path. The path must be a valid null-terminated UTF-8 string. The returned handle must be freed using cj_free. ```APIDOC ## cj_load_file ### Description Loads a model from a file path. Returns a pointer to a CJClassifierHandle on success, or null on failure. ### Signature `unsafe extern "C" fn cj_load_file(path: *const c_char) -> *mut CJClassifierHandle` ### Parameters - **path** (*const c_char) - Required - A valid null-terminated UTF-8 string representing the file path. ### Safety - The `path` argument must be a valid null-terminated UTF-8 string. - The returned pointer must be freed with `cj_free` to avoid memory leaks. ``` -------------------------------- ### Implement AsRawFd for Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Demonstrates implementing a trait requiring AsRawFd for an Arc-wrapped UdpSocket. ```rust use std::net::UdpSocket; use std::sync::Arc; trait MyTrait: AsRawFd { } impl MyTrait for Arc {} impl MyTrait for Box {} ``` -------------------------------- ### Implement AsFd for Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Demonstrates implementing a trait requiring AsFd for an Arc-wrapped UdpSocket. ```rust use std::net::UdpSocket; use std::sync::Arc; trait MyTrait: AsFd {} impl MyTrait for Arc {} impl MyTrait for Box {} ``` -------------------------------- ### Arc::as_ptr Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Provides a raw pointer to the data. ```APIDOC ## Arc::as_ptr ### Description Provides a raw pointer to the data. The counts are not affected in any way and the `Arc` is not consumed. The pointer is valid for as long as there are strong counts in the `Arc`. ``` -------------------------------- ### Creating Arc with Fallible Allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html try_new is an experimental API that returns a Result, allowing handling of allocation failures. ```rust #![feature(allocator_api)] use std::sync::Arc; let five = Arc::try_new(5)?; ``` -------------------------------- ### cj_load_bundled() Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_load_bundled.html Loads the bundled model. Returns a pointer to the classifier handle, or null if the operation fails. ```APIDOC ## Function: cj_load_bundled ### Description Loads the bundled model. Returns a pointer to a `CJClassifierHandle` on success, or null on failure. ### Signature `pub extern "C" fn cj_load_bundled() -> *mut CJClassifierHandle` ### Safety The returned pointer must be freed with `cj_free` to avoid memory leaks. ``` -------------------------------- ### Constructing Arc from Raw Pointer Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Demonstrates converting a raw pointer back into an Arc to prevent memory leaks. The pointer must have been previously obtained via Arc::into_raw. ```rust use std::sync::Arc; let x = Arc::new("hello".to_owned()); let x_ptr = Arc::into_raw(x); unsafe { // Convert back to an `Arc` to prevent leak. let x = Arc::from_raw(x_ptr); assert_eq!(&*x, "hello"); // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe. } // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! ``` ```rust use std::sync::Arc; let x: Arc<[u32]> = Arc::new([1, 2, 3]); let x_ptr: *const [u32] = Arc::into_raw(x); unsafe { let x: Arc<[u32; 3]> = Arc::from_raw(x_ptr.cast::<[u32; 3]>()); assert_eq!(&*x, &[1, 2, 3]); } ``` -------------------------------- ### Define CJ_LANGUAGES constant Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/constant.CJ_LANGUAGES.html Represents the three CJ languages in index order. ```rust pub const CJ_LANGUAGES: [CJLanguage; 3]; ``` -------------------------------- ### Arc::into_raw_with_allocator Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Consumes the Arc, returning the wrapped pointer and allocator. ```APIDOC ## Arc::into_raw_with_allocator ### Description Consumes the `Arc`, returning the wrapped pointer and allocator. To avoid a memory leak the pointer must be converted back to an `Arc` using `Arc::from_raw_in`. ``` -------------------------------- ### Creating Uninitialized Arc with Fallible Allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html try_new_uninit provides a fallible way to allocate uninitialized memory for an Arc. ```rust #![feature(allocator_api)] use std::sync::Arc; let mut five = Arc::::try_new_uninit()?; // Deferred initialization: Arc::get_mut(&mut five).unwrap().write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5); ``` -------------------------------- ### Results::to_short_string Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.Results.html Generates a compact string representation of the language scores. ```APIDOC ## pub fn to_short_string(&self) -> String ### Description Returns a compact, comma-separated representation of per-language relative scores, ordered from best to worst (e.g., “zh-hans:1.00,zh-hant:0.97,ja:0.85”). ### Returns - **String** - A formatted string containing the relative scores. ``` -------------------------------- ### Clone Arc with try_clone_from_ref_in Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Constructs a new Arc with a clone of the value in the provided allocator, returning an error if allocation fails. Requires the clone_from_ref and allocator_api features. ```rust #![feature(clone_from_ref)] #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let hello: Arc = Arc::try_clone_from_ref_in("hello", System)?; ``` -------------------------------- ### Construct Arc with custom allocator Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Creates a new Arc instance using a specified allocator. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let five = Arc::new_in(5, System); ``` -------------------------------- ### cj_load_file function signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_load_file.html The function signature for loading a model from a file path. It requires a null-terminated UTF-8 string and returns a pointer that must be freed. ```rust #[unsafe(no_mangle)] pub unsafe extern "C" fn cj_load_file( path: *const c_char, ) -> *mut CJClassifierHandle ``` -------------------------------- ### Clone Arc with clone_from_ref_in Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Constructs a new Arc with a clone of the value in the provided allocator. Requires the clone_from_ref and allocator_api features. ```rust #![feature(clone_from_ref)] #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let hello: Arc = Arc::clone_from_ref_in("hello", System); ``` -------------------------------- ### cj_load_file Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/index.html Loads a language classification model from a specified file path. ```APIDOC ## cj_load_file ### Description Loads a model from a file path. ### Parameters - **path** (const char*) - The file path to the model. ### Returns - **CJClassifierHandle*** - A pointer to the classifier instance, or NULL if loading fails. ``` -------------------------------- ### Creating Zeroed Arc with Fallible Allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html try_new_zeroed provides a fallible way to allocate and zero-initialize memory for an Arc. ```rust #![feature( allocator_api)] use std::sync::Arc; let zero = Arc::::try_new_zeroed()?; let zero = unsafe { zero.assume_init() }; assert_eq!(*zero, 0); ``` -------------------------------- ### cj_lang_index(lang: CJLanguage) -> Option Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/fn.cj_lang_index.html Returns the internal array index for a given CJ language. Returns None if the language is Unknown. ```APIDOC ## Function: cj_lang_index ### Description Returns the internal array index for a CJ language, or None for Unknown. ### Signature `pub fn cj_lang_index(lang: CJLanguage) -> Option` ### Parameters - **lang** (CJLanguage) - Required - The language identifier to look up. ### Returns - **Option** - The internal index of the language, or None if the language is not recognized. ``` -------------------------------- ### Construct Arc with uninitialized contents Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Requires the allocator_api and get_mut_unchecked features. Use unsafe blocks to initialize the memory before calling assume_init. ```rust #![feature(get_mut_unchecked)] #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let mut five = Arc::::new_uninit_in(System); let five = unsafe { // Deferred initialization: Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); five.assume_init() }; assert_eq!(*five, 5) ``` -------------------------------- ### cj_detect_detailed Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_detect_detailed.html Performs detailed language detection on a provided UTF-8 text buffer. ```APIDOC ## cj_detect_detailed ### Description Detects language with detailed results using a UTF-8 text buffer with an explicit length. ### Signature `unsafe extern "C" fn cj_detect_detailed(model: *const CJClassifierHandle, text: *const c_char, len: usize, result: *mut CJResult) -> CJLanguageCode` ### Parameters - **model** (*const CJClassifierHandle) - Required - A valid pointer to a classifier handle obtained from `cj_load_bundled` or `cj_load_file`. - **text** (*const c_char) - Required - A pointer to at least `len` bytes of valid UTF-8 text. - **len** (usize) - Required - The length of the text buffer. - **result** (*mut CJResult) - Required - A valid pointer to a `CJResult` struct where the output will be stored. ### Returns - **CJLanguageCode** - The detected language code. ``` -------------------------------- ### Construct Arc with zeroed contents Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Requires the allocator_api feature. The memory is initialized to zero bytes before the Arc is returned. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let zero = Arc::::new_zeroed_in(System); let zero = unsafe { zero.assume_init() }; assert_eq!(*zero, 0) ``` -------------------------------- ### cj_detect Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/index.html Detects the language of a provided UTF-8 text buffer. ```APIDOC ## cj_detect ### Description Detects the language of a UTF-8 text buffer with explicit length. ### Parameters - **model** (CJClassifierHandle*) - The classifier handle. - **text** (const char*) - The UTF-8 text buffer. - **length** (size_t) - The length of the text buffer. ### Returns - **CJLanguageCode** - The detected language code. ``` -------------------------------- ### Drop implementation for Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Demonstrates how the inner value is dropped only when the strong reference count reaches zero. ```rust use std::sync::Arc; struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("dropped!"); } } let foo = Arc::new(Foo); let foo2 = Arc::clone(&foo); drop(foo); // Doesn't print anything drop(foo2); // Prints "dropped!" ``` -------------------------------- ### Arc::try_clone_from_ref_in Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Constructs a new Arc with a clone of a value in the provided allocator, returning an error if allocation fails. ```APIDOC ## Arc::try_clone_from_ref_in ### Description Constructs a new `Arc` with a clone of `value` in the provided allocator, returning an error if allocation fails. ``` -------------------------------- ### Convert Cow to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Creates an atomically reference-counted pointer from a clone-on-write pointer by copying its content. ```rust let cow: Cow<'_, str> = Cow::Borrowed("eggplant"); let shared: Arc = Arc::from(cow); assert_eq!("eggplant", &shared[..]); ``` -------------------------------- ### Construct zeroed Arc with fallible allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Requires the allocator_api feature. Returns a Result and ensures memory is zeroed if allocation succeeds. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let zero = Arc::::try_new_zeroed_in(System)?; let zero = unsafe { zero.assume_init() }; assert_eq!(*zero, 0); ``` -------------------------------- ### Creating Uninitialized Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html new_uninit allocates memory for an Arc without initializing the contents, requiring manual initialization before use. ```rust use std::sync::Arc; let mut five = Arc::::new_uninit(); // Deferred initialization: Arc::get_mut(&mut five).unwrap().write(5); let five = unsafe { five.assume_init() }; assert_eq!(*five, 5) ``` -------------------------------- ### Construct Arc from raw pointer Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Reconstructs an Arc from a raw pointer previously obtained via into_raw_with_allocator. Requires the allocator_api feature. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let x = Arc::new_in("hello".to_owned(), System); let (x_ptr, alloc) = Arc::into_raw_with_allocator(x); unsafe { // Convert back to an `Arc` to prevent leak. let x = Arc::from_raw_in(x_ptr, System); assert_eq!(&*x, "hello"); // Further calls to `Arc::from_raw(x_ptr)` would be memory-unsafe. } // The memory was freed when `x` went out of scope above, so `x_ptr` is now dangling! ``` ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let x: Arc<[u32], _> = Arc::new_in([1, 2, 3], System); let x_ptr: *const [u32] = Arc::into_raw_with_allocator(x).0; unsafe { let x: Arc<[u32; 3], _> = Arc::from_raw_in(x_ptr.cast::<[u32; 3]>(), System); assert_eq!(&*x, &[1, 2, 3]); } ``` -------------------------------- ### Construct uninitialized Arc with fallible allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Requires the allocator_api and get_mut_unchecked features. Handles allocation failure while allowing deferred initialization. ```rust #![feature(allocator_api)] #![feature(get_mut_unchecked)] use std::sync::Arc; use std::alloc::System; let mut five = Arc::::try_new_uninit_in(System)?; let five = unsafe { // Deferred initialization: Arc::get_mut_unchecked(&mut five).as_mut_ptr().write(5); five.assume_init() }; assert_eq!(*five, 5); ``` -------------------------------- ### CJClassifier::detect Source: https://docs.rs/cjclassifier Classifies the provided input text into a supported CJK language. ```APIDOC ## CJClassifier::detect(&self, text: &str) -> CJLanguage ### Description Analyzes the input string and returns the detected language based on the internal probability models. ### Parameters - **text** (&str) - The input text to classify. ### Returns - **CJLanguage** - The detected language (ChineseSimplified, ChineseTraditional, or Japanese). ``` -------------------------------- ### Arc::clone_from_ref_in Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Constructs a new Arc with a clone of a value in the provided allocator. ```APIDOC ## Arc::clone_from_ref_in ### Description Constructs a new `Arc` with a clone of `value` in the provided allocator. ``` -------------------------------- ### Arc::allocator Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Returns a reference to the underlying allocator. ```APIDOC ## Arc::allocator ### Description Returns a reference to the underlying allocator. ``` -------------------------------- ### cj_lang_index function signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/fn.cj_lang_index.html Returns the internal array index for a CJ language, or None for Unknown. ```rust pub fn cj_lang_index(lang: CJLanguage) -> Option ``` -------------------------------- ### CJClassifier::detect Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/index.html Detects the language of the provided input text. ```APIDOC ## fn detect(&self, text: &str) -> CJLanguage ### Description Analyzes the input string and returns the detected CJK language. ### Parameters - **text** (&str) - The text to classify. ### Returns - **CJLanguage** - The detected language (ChineseSimplified, ChineseTraditional, or Japanese). ``` -------------------------------- ### Unwrap Arc with try_unwrap Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Demonstrates extracting the inner value from an Arc if it has exactly one strong reference. ```rust use std::sync::Arc; let x = Arc::new(3); assert_eq!(Arc::try_unwrap(x), Ok(3)); let x = Arc::new(4); let _y = Arc::clone(&x); assert_eq!(*Arc::try_unwrap(x).unwrap_err(), 4); ``` -------------------------------- ### cj_detect Function Signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_detect.html The function signature for cj_detect, requiring a valid classifier handle and a pointer to a UTF-8 encoded string. ```rust #[unsafe(no_mangle)] pub unsafe extern "C" fn cj_detect( model: *const CJClassifierHandle, text: *const c_char, len: usize, ) -> CJLanguageCode ``` -------------------------------- ### cj_detect Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_detect.html Detects the language of a UTF-8 text buffer using a provided classifier model handle. ```APIDOC ## cj_detect ### Description Detect the language of a UTF-8 text buffer with explicit length. ### Signature `unsafe extern "C" fn cj_detect(model: *const CJClassifierHandle, text: *const c_char, len: usize) -> CJLanguageCode` ### Parameters - **model** (*const CJClassifierHandle) - Required - A valid pointer to a classifier model obtained from `cj_load_bundled` or `cj_load_file`. - **text** (*const c_char) - Required - A pointer to a buffer containing at least `len` bytes of valid UTF-8 text. - **len** (usize) - Required - The length of the text buffer in bytes. ### Returns - **CJLanguageCode** - The detected language code. ``` -------------------------------- ### cj_load_bundled function signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_load_bundled.html The function is marked as unsafe and no_mangle, and it returns a mutable pointer to a CJClassifierHandle. ```rust #[unsafe(no_mangle)] pub extern "C" fn cj_load_bundled() -> *mut CJClassifierHandle ``` -------------------------------- ### Map Arc value with try_map Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Attempts to map the value inside an Arc, reusing the allocation if possible. Requires the smart_pointer_try_map feature. ```rust #![feature(smart_pointer_try_map)] use std::sync::Arc; let b = Arc::new(7); let new = Arc::try_map(b, |&i| u32::try_from(i)).unwrap(); assert_eq!(*new, 7); ``` -------------------------------- ### Consume Arc with into_raw_with_allocator Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Consumes the Arc and returns the wrapped pointer and allocator. The pointer must be converted back to an Arc to avoid memory leaks. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let x = Arc::new_in("hello".to_owned(), System); let (ptr, alloc) = Arc::into_raw_with_allocator(x); assert_eq!(unsafe { &*ptr }, "hello"); let x = unsafe { Arc::from_raw_in(ptr, alloc) }; assert_eq!(&*x, "hello"); ``` -------------------------------- ### Results Struct Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.Results.html The primary data structure for storing language detection results. ```rust pub struct Results { pub scores: Scores, pub total_scores: [f64; 3], pub boosts: [f64; 3], pub result: Option, pub gap: f64, } ``` -------------------------------- ### CJLanguage Methods Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/enum.CJLanguage.html Methods available on the CJLanguage enum for language identification and metadata retrieval. ```APIDOC ## Methods ### iso_code Returns the ISO 639-1-style language code (e.g., "zh-hans", "ja"). ### iso_code3 Returns the ISO 639-3-style language code (e.g., "zho-hans", "jpn"). ### is_chinese Returns true if the language is Chinese Simplified or Chinese Traditional. ### is_japanese Returns true if the language is Japanese. ### from_string Look up a language by name, ISO code, or alternate name (case-insensitive). ``` -------------------------------- ### cj_detect_detailed FFI Function Signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_detect_detailed.html The function signature for cj_detect_detailed, which requires valid pointers for the model, text buffer, and result struct. ```rust #[unsafe(no_mangle)] pub unsafe extern "C" fn cj_detect_detailed( model: *const CJClassifierHandle, text: *const c_char, len: usize, result: *mut CJResult, ) -> CJLanguageCode ``` -------------------------------- ### Convert Box to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Moves a boxed object into a new, reference-counted allocation. ```rust let unique: Box = Box::from("eggplant"); let shared: Arc = Arc::from(unique); assert_eq!("eggplant", &shared[..]); ``` -------------------------------- ### Check equality of Arcs Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Two Arcs are equal if their inner values are equal. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five == Arc::new(5)); ``` -------------------------------- ### Check inequality of Arcs Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Two Arcs are not equal if their inner values are not equal. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five != Arc::new(6)); ``` -------------------------------- ### Construct Arc with fallible allocation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Requires the allocator_api feature. Returns a Result to handle potential allocation failures. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let five = Arc::try_new_in(5, System)?; ``` -------------------------------- ### Arc::try_map Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Attempts to map the value in an Arc, reusing the allocation if possible. ```APIDOC ## Arc::try_map ### Description Attempts to map the value in an `Arc`, reusing the allocation if possible. `f` is called on a reference to the value in the `Arc`, and if the operation succeeds, the result is returned, also in an `Arc`. ``` -------------------------------- ### CJResult Struct Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/struct.CJResult.html The structure definition for CJResult, marked with #[repr(C)] for FFI compatibility. ```rust #[repr(C)] pub struct CJResult { pub language: CJLanguageCode, pub gap: f64, pub total_scores: [f64; 3], } ``` -------------------------------- ### Creating Zeroed Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html new_zeroed allocates memory for an Arc and initializes it with zero bytes. ```rust use std::sync::Arc; let zero = Arc::::new_zeroed(); let zero = unsafe { zero.assume_init() }; assert_eq!(*zero, 0) ``` -------------------------------- ### Inspect Arc reference counts Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Methods to retrieve the number of weak or strong references to an Arc allocation. ```rust use std::sync::Arc; let five = Arc::new(5); let _weak_five = Arc::downgrade(&five); // This assertion is deterministic because we haven't shared // the `Arc` or `Weak` between threads. assert_eq!(1, Arc::weak_count(&five)); ``` ```rust use std::sync::Arc; let five = Arc::new(5); let _also_five = Arc::clone(&five); // This assertion is deterministic because we haven't shared // the `Arc` between threads. assert_eq!(2, Arc::strong_count(&five)); ``` -------------------------------- ### Compare Arcs using Ord Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Compares two Arcs by calling cmp() on their inner values. ```rust use std::sync::Arc; use std::cmp::Ordering; let five = Arc::new(5); assert_eq!(Ordering::Less, five.cmp(&Arc::new(6))); ``` -------------------------------- ### cj_free Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_free.html Frees a classifier handle previously allocated by cj_load_bundled or cj_load_file. ```APIDOC ## cj_free ### Description Frees a classifier handle. This function releases the memory associated with the provided classifier handle. ### Signature `pub unsafe extern "C" fn cj_free(model: *mut CJClassifierHandle)` ### Safety `model` must be a valid pointer from `cj_load_bundled` or `cj_load_file`, or null (in which case this is a no-op). ``` -------------------------------- ### Compare Arc values with less-than-or-equal Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Checks if the inner value of the first Arc is less than or equal to the second. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five <= Arc::new(5)); ``` -------------------------------- ### Compare Arc values with partial_cmp Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Performs a partial comparison between two Arc instances by comparing their inner values. ```rust use std::sync::Arc; use std::cmp::Ordering; let five = Arc::new(5); assert_eq!(Some(Ordering::Less), five.partial_cmp(&Arc::new(6))); ``` -------------------------------- ### Constructing Cyclic Arc References Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Use new_cyclic to create an Arc that contains a weak reference to itself, preventing memory leaks in circular structures. ```rust use std::sync::{Arc, Weak}; struct Gadget { me: Weak, } impl Gadget { /// Constructs a reference counted Gadget. fn new() -> Arc { // `me` is a `Weak` pointing at the new allocation of the // `Arc` we're constructing. Arc::new_cyclic(|me| { // Create the actual struct here. Gadget { me: me.clone() } }) } /// Returns a reference counted pointer to Self. fn me(&self) -> Arc { self.me.upgrade().unwrap() } } ``` -------------------------------- ### Results::clear Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.Results.html Resets the current Results instance to its default state. ```APIDOC ## pub fn clear(&mut self) ### Description Clears the current detection results and resets scores within the instance. ``` -------------------------------- ### Create Default Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Creates a new Arc containing the default value of the inner type. ```rust use std::sync::Arc; let x: Arc = Default::default(); assert_eq!(*x, 0); ``` -------------------------------- ### CJClassifier::load Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.CJClassifier.html Returns a cached classifier loaded from the bundled model. Subsequent calls return the same instance. Thread-safe. ```APIDOC ## pub fn load() -> Result> ### Description Returns a cached classifier loaded from the bundled model. Subsequent calls return the same instance. Thread-safe. ``` -------------------------------- ### Access Arc raw pointer with as_ptr Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Provides a raw pointer to the data without affecting reference counts or consuming the Arc. ```rust use std::sync::Arc; let x = Arc::new("hello".to_owned()); let y = Arc::clone(&x); let x_ptr = Arc::as_ptr(&x); assert_eq!(x_ptr, Arc::as_ptr(&y)); assert_eq!(unsafe { &*x_ptr }, "hello"); ``` -------------------------------- ### CJClassifier::set_tolerated_kana_threshold Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.CJClassifier.html Sets the kana fraction threshold used to classify text as Japanese. ```APIDOC ## pub fn set_tolerated_kana_threshold(&mut self, threshold: f64) ### Description Set the kana fraction threshold above which text is classified as Japanese. ``` -------------------------------- ### Clone an Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Creates a new pointer to the same allocation, incrementing the strong reference count. ```rust use std::sync::Arc; let five = Arc::new(5); let _ = Arc::clone(&five); ``` -------------------------------- ### Consuming Arc into Raw Pointer Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Consumes the Arc and returns the underlying raw pointer. The pointer must be converted back to an Arc to avoid memory leaks. ```rust use std::sync::Arc; let x = Arc::new("hello".to_owned()); let x_ptr = Arc::into_raw(x); assert_eq!(unsafe { &*x_ptr }, "hello"); ``` -------------------------------- ### Guarantee inner value extraction with into_inner Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Shows how into_inner guarantees that exactly one thread receives the inner value when multiple threads attempt to extract it from clones. ```rust use std::sync::Arc; let x = Arc::new(3); let y = Arc::clone(&x); // Two threads calling `Arc::into_inner` on both clones of an `Arc`: let x_thread = std::thread::spawn(|| Arc::into_inner(x)); let y_thread = std::thread::spawn(|| Arc::into_inner(y)); let x_inner_value = x_thread.join().unwrap(); let y_inner_value = y_thread.join().unwrap(); // One of the threads is guaranteed to receive the inner value: assert!(matches!( (x_inner_value, y_inner_value), (None, Some(3)) | (Some(3), None) )); // The result could also be `(None, None)` if the threads called // `Arc::try_unwrap(x).ok()` and `Arc::try_unwrap(y).ok()` instead. ``` -------------------------------- ### Compare Arc values with less-than Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Checks if the inner value of the first Arc is less than the second. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five < Arc::new(6)); ``` -------------------------------- ### Compare Arc values with greater-than-or-equal Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Checks if the inner value of the first Arc is greater than or equal to the second. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five >= Arc::new(5)); ``` -------------------------------- ### Comparing Arc Allocations Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Checks if two Arc pointers refer to the same allocation, ignoring metadata for dyn Trait pointers. ```rust use std::sync::Arc; let five = Arc::new(5); let same_five = Arc::clone(&five); let other_five = Arc::new(5); assert!(Arc::ptr_eq(&five, &same_five)); assert!(!Arc::ptr_eq(&five, &other_five)); ``` -------------------------------- ### Downgrade Arc to Weak pointer Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Creates a new Weak pointer to the allocation managed by the Arc. ```rust use std::sync::Arc; let five = Arc::new(5); let weak_five = Arc::downgrade(&five); ``` -------------------------------- ### Convert T to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Converts a value into an Arc, equivalent to calling Arc::new(t). ```rust let x = 5; let arc = Arc::new(5); assert_eq!(Arc::from(x), arc); ``` -------------------------------- ### CJClassifierHandle Struct Representation Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html The internal struct representation of CJClassifierHandle. ```rust pub struct CJClassifierHandle { /* private fields */ } ``` -------------------------------- ### Prevent stack overflow with into_inner in Drop Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Uses into_inner within a custom Drop implementation to safely deallocate a linked list without recursion. ```rust use std::sync::Arc; // Definition of a simple singly linked list using `Arc`: #[derive(Clone)] struct LinkedList(Option>>); struct Node(T, Option>>); // Dropping a long `LinkedList` relying on the destructor of `Arc` // can cause a stack overflow. To prevent this, we can provide a // manual `Drop` implementation that does the destruction in a loop: impl Drop for LinkedList { fn drop(&mut self) { let mut link = self.0.take(); while let Some(arc_node) = link.take() { if let Some(Node(_value, next)) = Arc::into_inner(arc_node) { link = next; } } } } // Implementation of `new` and `push` omitted impl LinkedList { /* ... */ } // The following code could have still caused a stack overflow // despite the manual `Drop` impl if that `Drop` impl had used // `Arc::try_unwrap(arc).ok()` instead of `Arc::into_inner(arc)`. // Create a long list and clone it let mut x = LinkedList::new(); let size = 100000; for i in 0..size { x.push(i); // Adds i to the front of x } let y = x.clone(); // Drop the clones in parallel let x_thread = std::thread::spawn(|| drop(x)); let y_thread = std::thread::spawn(|| drop(y)); x_thread.join().unwrap(); y_thread.join().unwrap(); ``` -------------------------------- ### cj_free function signature Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/fn.cj_free.html The function signature for cj_free, which is used to free a classifier handle. ```rust #[unsafe(no_mangle)] pub unsafe extern "C" fn cj_free(model: *mut CJClassifierHandle) ``` -------------------------------- ### Scores Struct Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/struct.Scores.html Defines the data structure used to accumulate per-language scoring metrics during text processing. ```rust pub struct Scores { pub unigram_scores: [f64; 3], pub bigram_scores: [f64; 3], pub unigram_hits_per_lang: [i32; 3], pub bigram_hits_per_lang: [i32; 3], pub kana_count: i32, pub cj_char_count: i32, } ``` -------------------------------- ### CJLanguage Enum Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/enum.CJLanguage.html The core enum structure representing supported languages with integer discriminants. ```rust #[repr(C)] pub enum CJLanguage { Unknown = 0, ChineseSimplified = 1, ChineseTraditional = 2, Japanese = 3, } ``` -------------------------------- ### Unwrapping or Cloning Arc Values Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Returns the inner value if the Arc has a unique reference, otherwise returns a clone of the inner value. ```rust let inner = String::from("test"); let ptr = inner.as_ptr(); let arc = Arc::new(inner); let inner = Arc::unwrap_or_clone(arc); // The inner value was not cloned assert!(ptr::eq(ptr, inner.as_ptr())); let arc = Arc::new(inner); let arc2 = arc.clone(); let inner = Arc::unwrap_or_clone(arc); // Because there were 2 references, we had to clone the inner value. assert!(!ptr::eq(ptr, inner.as_ptr())); // `arc2` is the last reference, so when we unwrap it we get back // the original `String`. let inner = Arc::unwrap_or_clone(arc2); assert!(ptr::eq(ptr, inner.as_ptr())); ``` -------------------------------- ### Compare Arc values with greater-than Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Checks if the inner value of the first Arc is greater than the second. ```rust use std::sync::Arc; let five = Arc::new(5); assert!(five > Arc::new(4)); ``` -------------------------------- ### CJLanguageCode Type Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJLanguageCode.html The type alias definition for CJLanguageCode as an i32 integer. ```rust pub type CJLanguageCode = i32; ``` -------------------------------- ### Mapping Arc Values Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Arc::map allows transforming the inner value of an Arc while potentially reusing the allocation. ```rust #![feature(smart_pointer_try_map)] use std::sync::Arc; let r = Arc::new(7); let new = Arc::map(r, |i| i + 7); assert_eq!(*new, 14); ``` -------------------------------- ### CJClassifierHandle Definition Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html The definition of the CJClassifierHandle type alias. ```rust pub type CJClassifierHandle = Arc; ``` -------------------------------- ### Increment strong reference count Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Manually increments the strong reference count for an Arc managed by a raw pointer. Requires the allocator_api feature. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let five = Arc::new_in(5, System); unsafe { let (ptr, _alloc) = Arc::into_raw_with_allocator(five); Arc::increment_strong_count_in(ptr, System); // This assertion is deterministic because we haven't shared // the `Arc` between threads. let five = Arc::from_raw_in(ptr, System); assert_eq!(2, Arc::strong_count(&five)); } ``` -------------------------------- ### Making Mutable References to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Provides a mutable reference to the inner value, cloning the data if multiple references exist (clone-on-write). ```rust use std::sync::Arc; let mut data = Arc::new(5); *Arc::make_mut(&mut data) += 1; // Won't clone anything let mut other_data = Arc::clone(&data); // Won't clone inner data *Arc::make_mut(&mut data) += 1; // Clones inner data *Arc::make_mut(&mut data) += 1; // Won't clone anything *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything // Now `data` and `other_data` point to different allocations. assert_eq!(*data, 8); assert_eq!(*other_data, 12); ``` ```rust use std::sync::Arc; let mut data = Arc::new(75); let weak = Arc::downgrade(&data); assert!(75 == *data); assert!(75 == *weak.upgrade().unwrap()); *Arc::make_mut(&mut data) += 1; assert!(76 == *data); assert!(weak.upgrade().is_none()); ``` -------------------------------- ### Check Arc uniqueness Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Determines if the current Arc is the sole owner of the allocation. ```rust #![feature(arc_is_unique)] use std::sync::Arc; let x = Arc::new(3); assert!(Arc::is_unique(&x)); let y = Arc::clone(&x); assert!(!Arc::is_unique(&x)); drop(y); // Weak references also count, because they could be upgraded at any time. let z = Arc::downgrade(&x); assert!(!Arc::is_unique(&x)); ``` ```rust #![feature(arc_is_unique)] use std::sync::Arc; let arc = Arc::new(5); let pointer: *const i32 = &*arc; assert!(Arc::is_unique(&arc)); assert_eq!(unsafe { *pointer }, 5); ``` -------------------------------- ### Incrementing Strong Reference Count Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Increments the strong reference count of an Arc via its raw pointer. The pointer must be valid and obtained from Arc::into_raw. ```rust use std::sync::Arc; let five = Arc::new(5); unsafe { let ptr = Arc::into_raw(five); Arc::increment_strong_count(ptr); // This assertion is deterministic because we haven't shared // the `Arc` between threads. let five = Arc::from_raw(ptr); assert_eq!(2, Arc::strong_count(&five)); } ``` -------------------------------- ### Unsafe mutable access to Arc Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Provides unchecked mutable access to the inner value. Requires ensuring no other pointers exist or are accessed. ```rust #![feature(get_mut_unchecked)] use std::sync::Arc; let mut x = Arc::new(String::new()); unsafe { Arc::get_mut_unchecked(&mut x).push_str("foo") } assert_eq!(*x, "foo"); ``` ```rust #![feature(get_mut_unchecked)] use std::sync::Arc; let x: Arc = Arc::from("Hello, world!"); let mut y: Arc<[u8]> = x.clone().into(); unsafe { // this is Undefined Behavior, because x's inner type is str, not [u8] Arc::get_mut_unchecked(&mut y).fill(0xff); // 0xff is invalid in UTF-8 } println!("{}", &*x); // Invalid UTF-8 in a str ``` ```rust #![feature(get_mut_unchecked)] use std::sync::Arc; let x: Arc<&str> = Arc::new("Hello, world!"); { let s = String::from("Oh, no!"); let mut y: Arc<&str> = x.clone(); unsafe { // this is Undefined Behavior, because x's inner type // is &'long str, not &'short str *Arc::get_mut_unchecked(&mut y) = &s; } } println!("{}", &*x); // Use-after-free ``` -------------------------------- ### Decrementing Arc Strong Count Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Decrements the strong reference count of an Arc instance using a specific allocator. Requires the allocator_api feature. ```rust #![feature(allocator_api)] use std::sync::Arc; use std::alloc::System; let five = Arc::new_in(5, System); unsafe { let (ptr, _alloc) = Arc::into_raw_with_allocator(five); Arc::increment_strong_count_in(ptr, System); // Those assertions are deterministic because we haven't shared // the `Arc` between threads. let five = Arc::from_raw_in(ptr, System); assert_eq!(2, Arc::strong_count(&five)); Arc::decrement_strong_count_in(ptr, System); assert_eq!(1, Arc::strong_count(&five)); } ``` -------------------------------- ### Decrementing Strong Reference Count Source: https://docs.rs/cjclassifier/0.1.0/cjclassifier/ffi/type.CJClassifierHandle.html Decrements the strong reference count of an Arc via its raw pointer. This can be used to release the final Arc instance. ```rust use std::sync::Arc; let five = Arc::new(5); unsafe { let ptr = Arc::into_raw(five); Arc::increment_strong_count(ptr); // Those assertions are deterministic because we haven't shared // the `Arc` between threads. let five = Arc::from_raw(ptr); assert_eq!(2, Arc::strong_count(&five)); Arc::decrement_strong_count(ptr); assert_eq!(1, Arc::strong_count(&five)); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.