Добрый день.
Опять прошу помощи.
Есть структура в ней HashMap в которой лежат ссылки, код внизу, не дает файл rs прицепить…
При компиляции получаю ошибку, что ниже.
Не могу разобраться как мне вернуть ссылку из этого HashMap.
Объясните, пожалуйста, в чем проблема?
Ну и в идеале, как работают указываемые лайфтаймы в возвращаемых ссылках или поделитесь ссылкой что читать.
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
–> src/executor/mod.rs:62:37
|
62 | let ref res = self.func_map.get( lib_name );
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 61:5…
–> src/executor/mod.rs:61:5
|
61 | / fn get_lib<'lf>( &self, lib_name: &str ) -> &'lf libloading::Library {
62 | | let ref res = self.func_map.get( lib_name );
63 | | let mut result : &libloading::Library;
64 | | if res.is_some() {
… |
72 | | return result
73 | | }
| |_____^
note: …so that reference does not outlive borrowed content
–> src/executor/mod.rs:62:23
|
62 | let ref res = self.func_map.get( lib_name );
| ^^^^^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'lf as defined on the method body at 61:16…
–> src/executor/mod.rs:61:16
|
61 | fn get_lib<'lf>( &self, lib_name: &str ) -> &'lf libloading::Library {
| ^^^
note: …so that reference does not outlive borrowed content
–> src/executor/mod.rs:72:16
|
72 | return result
| ^^^^^^
use std::collections::{ HashMap };
use std::result::{ Result };
use crate::traits::WorkWithHashMap;
extern crate libloading;
extern crate json;
type RunFunc = unsafe fn( &str, &str ) -> String;
pub struct Executor{
func_desc_map: HashMap< String, String>,
func_map: HashMap< String, libloading::Library >
}
impl Executor{
fn split_action( action: &str ) -> ( &str, &str ) {
let offset = action.find( “.” ).unwrap_or( action.len() );
let ( first, mut last) = action.split_at(offset);
last = &last[1…];
return ( first, last );
}
fn split_method( method: &str ) -> ( &str, &str ) {
let offset = method.find( '(' ).unwrap_or( method.len() );
let ( first, mut last) = method.split_at(offset);
last = &last[1..];
let offset1 = last.find( ')' ).unwrap_or( last.len() );
let ( first1, last1 ) = last.split_at(offset1);
return ( first, first1 );
}
fn create_args<'life_time>( args_desc: &'life_time str, args: &'life_time str ) -> Result<String, &'life_time str> {
let desc_vec: Vec<&str> = args_desc.split( ',' ).collect();
let args_vec: Vec<&str> = args.split( ',' ).collect();
if desc_vec.len() != args_vec.len() {
return Err( "Description arguments and arguments have different size!" );
}
let mut res_args = json::JsonValue::new_array();
for ( i, desc ) in desc_vec.iter().enumerate() {
let arg = args_vec[ i ];
println!( "{}-ые элементы имеет значение {} {}", i, desc, arg );
res_args.push(
json::object!{
"type" => desc,
"value" => arg
}
);
}
let mut res = json::JsonValue::new_object();
res[ "Args" ] = res_args.into();
return Ok( json::stringify( res ) );
}
fn get_params<'lf>( &self, action: &'lf str, args: &'lf str ) -> ( &'lf str, &'lf str, &'lf str ) {
let ( lib, method ) = Executor::split_action( action );
let ( real_method, args_desc ) = Executor::split_method( method );
let arsg = Executor::create_args( args_desc, args );
return ( lib, real_method, args );
}
fn get_lib<'lf>( &self, lib_name: &str ) -> &'lf libloading::Library {
let res = self.func_map.get( lib_name );
let mut result : &libloading::Library;
if res.is_some() {
result = res.unwrap();
}
else {
let lib = libloading::Library::new( lib_name ).unwrap();
self.func_map.insert( lib_name.to_string(), lib );
result = self.func_map.get( lib_name ).unwrap();
}
return result
}
pub fn run( &self, action: &str, args: &str ) -> String {
let ( lib, method, args ) = self.get_params( action, args );
let real_lib = self.get_lib( lib );
unsafe {
let func: libloading::Symbol<RunFunc> = real_lib.get(b"run").unwrap();
return func( method, args );
}
}
}
impl WorkWithHashMap for Executor{
fn new<'lf>( filename: &str ) ->Executor {
let mut new_executor = Executor{ func_desc_map: HashMap::new(), func_map: HashMap::new() };
new_executor.load( filename );
return new_executor;
}
fn get_hash_map( &mut self ) -> &mut HashMap<String, String>{
return &mut self.func_desc_map;
}
}