diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /toolkit/components/cascade_bloom_filter/src/lib.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/cascade_bloom_filter/src/lib.rs')
-rw-r--r-- | toolkit/components/cascade_bloom_filter/src/lib.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/toolkit/components/cascade_bloom_filter/src/lib.rs b/toolkit/components/cascade_bloom_filter/src/lib.rs new file mode 100644 index 0000000000..494b67b68d --- /dev/null +++ b/toolkit/components/cascade_bloom_filter/src/lib.rs @@ -0,0 +1,56 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +extern crate nserror; +extern crate nsstring; +extern crate rust_cascade; +extern crate thin_vec; +#[macro_use] +extern crate xpcom; + +use nserror::{nsresult, NS_ERROR_INVALID_ARG, NS_ERROR_NOT_INITIALIZED, NS_OK}; +use nsstring::nsACString; +use rust_cascade::Cascade; +use std::cell::RefCell; +use thin_vec::ThinVec; +use xpcom::interfaces::nsICascadeFilter; +use xpcom::{xpcom_method, RefPtr}; + +#[xpcom(implement(nsICascadeFilter), nonatomic)] +pub struct CascadeFilter { + filter: RefCell<Option<Cascade>>, +} + +impl CascadeFilter { + fn new() -> RefPtr<CascadeFilter> { + CascadeFilter::allocate(InitCascadeFilter { + filter: RefCell::new(None), + }) + } + xpcom_method!(set_filter_data => SetFilterData(data: *const ThinVec<u8>)); + + fn set_filter_data(&self, data: &ThinVec<u8>) -> Result<(), nsresult> { + let filter = Cascade::from_bytes(data.to_vec()) + .unwrap_or(None) + .ok_or(NS_ERROR_INVALID_ARG)?; + self.filter.borrow_mut().replace(filter); + Ok(()) + } + + xpcom_method!(has => Has(key: *const nsACString) -> bool); + + fn has(&self, key: &nsACString) -> Result<bool, nsresult> { + match self.filter.borrow().as_ref() { + None => Err(NS_ERROR_NOT_INITIALIZED), + Some(filter) => Ok(filter.has(key.to_vec())), + } + } +} + +#[no_mangle] +pub unsafe extern "C" fn cascade_filter_construct(result: &mut *const nsICascadeFilter) { + let inst: RefPtr<CascadeFilter> = CascadeFilter::new(); + *result = inst.coerce::<nsICascadeFilter>(); + std::mem::forget(inst); +} |