Vendor in a purple-sys wrapper
This code comes from https://github.com/Flared/purple-icq/tree/master/src/purple It is GPL-3 licensed
This commit is contained in:
45
src/purple/connection/connections.rs
Normal file
45
src/purple/connection/connections.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use super::protocol_data::ProtocolData;
|
||||
use crate::purple::ffi::AsMutPtr;
|
||||
use crate::purple::Connection;
|
||||
use std::os::raw::c_void;
|
||||
|
||||
pub struct Connections<T> {
|
||||
protocol_datas: std::collections::HashSet<*mut ProtocolData<T>>,
|
||||
}
|
||||
|
||||
impl<T> Connections<T> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
protocol_datas: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe fn add(&mut self, connection: Connection, data: T) {
|
||||
let account = connection.get_account();
|
||||
let data_ptr = Box::new(ProtocolData::<T> {
|
||||
account,
|
||||
connection,
|
||||
data,
|
||||
});
|
||||
let data_raw_ptr = Box::into_raw(data_ptr);
|
||||
connection.set_protocol_data(data_raw_ptr as *mut c_void);
|
||||
self.protocol_datas.insert(data_raw_ptr);
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, connection: Connection) {
|
||||
let protocol_data_ptr = connection.get_protocol_data() as *mut ProtocolData<T>;
|
||||
self.protocol_datas.remove(&protocol_data_ptr);
|
||||
// Retake ownership of the protocol data to drop its data.
|
||||
unsafe { Box::from_raw(protocol_data_ptr) };
|
||||
}
|
||||
|
||||
pub fn get<'b, P>(&mut self, mut ptr: P) -> Option<&'b mut ProtocolData<T>>
|
||||
where
|
||||
P: AsMutPtr<PtrType = ProtocolData<T>>,
|
||||
{
|
||||
self.protocol_datas
|
||||
.get(&ptr.as_mut_ptr())
|
||||
.cloned()
|
||||
.map(|p| unsafe { &mut *p })
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user