From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- toolkit/components/cascade_bloom_filter/src/lib.rs | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 toolkit/components/cascade_bloom_filter/src/lib.rs (limited to 'toolkit/components/cascade_bloom_filter/src') 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>, +} + +impl CascadeFilter { + fn new() -> RefPtr { + CascadeFilter::allocate(InitCascadeFilter { + filter: RefCell::new(None), + }) + } + xpcom_method!(set_filter_data => SetFilterData(data: *const ThinVec)); + + fn set_filter_data(&self, data: &ThinVec) -> 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 { + 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::new(); + *result = inst.coerce::(); + std::mem::forget(inst); +} -- cgit v1.2.3