These are chat archives for rust-lang/rust

26th
Oct 2018
rsb007
@rsb007
Oct 26 2018 08:36
how to conver json string type into Jsonvalue type?
Michal 'vorner' Vaner
@vorner
Oct 26 2018 08:38
@erduranogulcan It depends on what you want, mostly. It's nice to have other options ready ‒ like if you want to look through some log files and it's one-time thing, then you probably don't want to bother writing it in a compiled language. But if you mostly do one kind of services, then doing them in similar way may be good thing. But certainly, Rust isn't the right tool for every job.
rsb007
@rsb007
Oct 26 2018 08:39
i wnat to convert jsonstring value into json
Michal 'vorner' Vaner
@vorner
Oct 26 2018 08:39
@rsb007 Have you looked at serde-json? It probably even has an example how to do exactly that.
Michael(LAI)
@Michael-Lfx
Oct 26 2018 08:55
Hi, I am new to Rust. Is there a better way to refactor the fellowing code.
#[no_mangle]
pub extern "C" fn glDeleteFramebuffers(n: GLsizei, framebuffers: *const GLuint) {
    info!("glDeleteFramebuffers(n = {}, framebuffers = {:p})", n, framebuffers);
    return_if_no_current_context!();

    if n < 0 {
        current_context_mut().state_manager.record_error(ErrorType::InvalidValue);
        return;
    }

    if framebuffers == ptr::null() {
        warn!("framebuffers is nullptr");
        return;
    }

    let objects = unsafe { slice::from_raw_parts(framebuffers, n as usize) };
    // todo: find out a better implementation for `current_context_mut().write_frame_buffer` and `current_context().system_frame_buffer`
    let frame_buffers = &mut current_context_mut().resource_manager.frame_buffers;
    for object_name in objects {
        let object_name = *object_name;
        debug!("object_name = {} in loop", object_name);

        if object_name > 0 && frame_buffers.is_object_exists(object_name) {
            // state check
            {
                let frame_buffer_will_be_freed = frame_buffers.get_object_mut(object_name);
                if current_context_mut().write_frame_buffer == frame_buffer_will_be_freed {
                    if frame_buffer_will_be_freed.is_in_draw_state() {
                        glFinish();
                    }

                    // restore operation
                    current_context_mut().write_frame_buffer = current_context().system_frame_buffer;
                    unsafe { &mut *current_context_mut().write_frame_buffer }.set_state_idle();

                    current_context_mut().state_manager.active_objects.frame_buffer = (0, current_context().write_frame_buffer);

                    let mut pipeline = &mut current_context_mut().pipeline;
                    pipeline.update_pipeline = true;
                    pipeline.update_viewport_state = true;
                }
            }

            frame_buffers.deallocate(object_name);
        }
    }
}
I have a lot unsafe code…
Denis Lisov
@tanriol
Oct 26 2018 09:05
Why is unsafe needed in unsafe { &mut *current_context_mut().write_frame_buffer }.set_state_idle();?
Is this pattern with lots of current_context_mut() common in this code?
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:05
write_frame_buffer is *mut FrameBuffer
@tanriol Yeah, current_context_mut() itself is converting *mut context to &mut Context
pub fn current_context<'a>() -> &'a Context {
    unsafe { &*CURRENT_CONTEXT }
}

pub fn current_context_mut<'a>() -> &'a mut Context {
    unsafe { &mut *CURRENT_CONTEXT }
}
Denis Lisov
@tanriol
Oct 26 2018 09:09
Looks very unsafe. What's the type of CURRENT_CONTEXT?
Also, I hope you don't require absolutely no overhead?
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:12
pub static mut CURRENT_CONTEXT: *mut Context = std::ptr::null_mut();
Denis Lisov
@tanriol
Oct 26 2018 09:14
First, IIUC, OpenGL current context is supposed to be per-thread (a thread-local, not a global).
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:14

Also, I hope you don't require absolutely no overhead?

@tanriol Could you explain it with more detail, please?

First, IIUC, OpenGL current context is supposed to be per-thread (a thread-local, not a global).

Yeah, it will be set by eglMakeCurrent. The EGL project is under developing.

Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:21
The EGL project has its own context aka EGLContext which takes the previous Context as one of its member. The EGLContext is the true context we normally use.
Denis Lisov
@tanriol
Oct 26 2018 09:21
For example, I'd suggest that CURRENT_CONTEXT would be much better as
thread_local! {
    static CURRENT_CONTEXT: RefCell<Option<Box<Context>>> = RefCell::new(None);
}
I'd still suggest not using raw pointers in the internal structures :-)
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:22
In that case RefCell CURRENT_CONTEXT can not be replaced with the new one.
Denis Lisov
@tanriol
Oct 26 2018 09:24
Why? *CURRENT_CONTEXT.borrow_mut() = Some(ctx); should work, no?
Speaking about refactoring, I'd suggest creating a helper usable like this
#[no_mangle]
pub extern "C" fn glDeleteFramebuffers(n: GLsizei, framebuffers: *const GLuint) {
    info!("glDeleteFramebuffers(n = {}, framebuffers = {:p})", n, framebuffers);
    with_current_context(|context: &mut Context| -> Result<(), ErrorType> {
        /* ... */
    })
}
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:37

Why? *CURRENT_CONTEXT.borrow_mut() = Some(ctx); should work, no?

At first I define it like the following. And it cannot be replaced…

lazy_static! {
    pub(crate) static ref CURRENT_CONTEXT: Arc<Mutex<Context>> = Arc::new(Mutex::new(Context::new()));
}
Denis Lisov
@tanriol
Oct 26 2018 09:39
Sounds weird... it should be able to be replaced. Probably you've done something wrong.
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:41
CURRENT_CONTEXT = new_ CURRENT_CONTEXT will get a compilation error.
red75prime
@red75prime
Oct 26 2018 09:42
*CURRENT_CONTEXT.lock().unwrap() = ...
Michael(LAI)
@Michael-Lfx
Oct 26 2018 09:46

*CURRENT_CONTEXT.lock().unwrap() = …

Thanks. I will try it later.

Michael(LAI)
@Michael-Lfx
Oct 26 2018 10:26
@tanriol Thanks. with_current_context looks good. I think we should pass log string to that closure too if we need change info! to trace! or something else .
Lyle Mantooth
@IslandUsurper
Oct 26 2018 11:20
@Michael-Lfx, not sure how important it is for your type, but if framebuffers.is_null() may work better: https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null
Sylwester Rąpała
@xoac
Oct 26 2018 13:23
This message was deleted

I have a problem with serde I use this in package1 then I use package2 that reexport some item from package1 and then use package2 in package3. Now package3::ItemFrom1 dosen't support serde.

package1:

[dependencies]
bytes = { version = "0.4", features=["serde"] }
chrono = { version = "0.4", features=["serde"] }
serde = { version = "^1.0.66", features = ["derive"] }

package2:

[dependencies]
package1 = { path = "package1", version="0.2"}
bytes = { version="^0.4", features = ["serde"] }

package3

[dependencies]
package2 {path = "package2"}
Sylwester Rąpała
@xoac
Oct 26 2018 13:29

when I generate doc for package2 Item from package1 doesn't support Serialize and Deseraialize. If I add serde as a feature to package1
like this:

package1 = { path = "package1", version="0.2", features = ["serde"]}

it works

but with this:
warning: Package `package1 v0.2.0 ( .. /package3/package2/package1)` does not have feature `serde`. It has a required dependency with that name, but only optional dependencies can be used as features. This is currently a warning to ease the transition, but it will become an error in the future.
Michael(LAI)
@Michael-Lfx
Oct 26 2018 14:05

@Michael-Lfx, not sure how important it is for your type, but if framebuffers.is_null() may work better: https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null

@IslandUsurper Thanks. framebuffers is definitely not nullptr if no thing goes wrong.

Ingvar Stepanyan
@RReverser
Oct 26 2018 14:56
@xoac Well you need to add serde to the package in which you want to use Serialize and Deserialize too, not just the one you're exporting it from
so if you want to use it in package2, add serde to it as well
Ichoran
@Ichoran
Oct 26 2018 16:00
@erduranogulcan - I actually think you can get away with using Rust for practically everything. Get Jupyter set up with the EvCxR Rust kernel and you basically have a REPL and can do your quick stuff there. Use play.rust-lang.org to try out quick ideas. cargo new gets quick little projects up to speed fast. The only things you'll be missing are libraries that you can't get conveniently from Rust (not terribly many, but for instance there's nothing to read and write exotic image formats yet), and web UI stuff (where even if you use Rust, you really need to know enough JS to understand how the JS library works).
I'm not sure I'd recommend this as ideal, but it ought to work pretty well.
(Also, different people like different things. In contrast to @vorner, writing a one-time log-file reader is one of my favorite places to use a compiled language because the reader tends to be fast and because I can get away with very little testing thanks to the compiler catching stuff for me.)
Sylwester Rąpała
@xoac
Oct 26 2018 16:10
@RReverser dosen't work. If I remove features =[ "serde"] z package1 and add serde = "1" to package2 as dep this dosen't compile :(
AlxGDev
@AlxGDev
Oct 26 2018 22:56
Hello all, anyone here got experience with clippy? I installed it via rustup component add clippy-preview and tried running cargo clippy on my project but I am getting errors from a dependency (actix-web) like "error[E0658]: scoped lint clippy::needless_pass_by_value is experimental (see issue #44690)" so my project doesnt compile and clippy doesn't get to check the code i wrote myself. Is there a way to disable scoped lints or some other fix?
Denis Lisov
@tanriol
Oct 26 2018 23:00
Probably actix-web expects clippy to be used from the nightly toolchain only.
You can install it and run the nightly clippy
AlxGDev
@AlxGDev
Oct 26 2018 23:01
ah thanks, i'll try that
AlxGDev
@AlxGDev
Oct 26 2018 23:18
Unfortunately I get the error " rustup component add --toolchain=nightly clippy-preview
error: toolchain 'nightly-x86_64-pc-windows-msvc' does not contain component 'clippy-preview' for target 'x86_64-pc-windows-msvc'" so it seems there's no clippy component for the nightly toolchain, at least for windows
Jason Ozias
@CraZySacX
Oct 26 2018 23:36
@AlxGDev Try an #[allow(clippy::needless_pass_by_value)] on the use clause or extern crate clause.
AlxGDev
@AlxGDev
Oct 26 2018 23:41
did a complete reinstall of rust, that fixed it